[v2,3/6] qapi: add nbd-server-remove

Message ID 20171207155102.66622-4-vsementsov@virtuozzo.com
State New
Headers show
Series
  • nbd export qmp interface
Related show

Commit Message

Vladimir Sementsov-Ogievskiy Dec. 7, 2017, 3:50 p.m.
Add command for export removing. It is needed for cases when we
don't want to keep export after the operation on it was completed.
The other example is temporary node, created with blockdev-add.
If we want to delete it we should firstly remove corresponding
NBD export.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 qapi/block.json     | 18 ++++++++++++++++++
 include/block/nbd.h |  3 ++-
 blockdev-nbd.c      | 29 ++++++++++++++++++++++++++++-
 nbd/server.c        | 25 ++++++++++++++++++++++---
 4 files changed, 70 insertions(+), 5 deletions(-)

Comments

Eric Blake Jan. 9, 2018, 7:52 p.m. | #1
On 12/07/2017 09:50 AM, Vladimir Sementsov-Ogievskiy wrote:
> Add command for export removing. It is needed for cases when we
> don't want to keep export after the operation on it was completed.
> The other example is temporary node, created with blockdev-add.
> If we want to delete it we should firstly remove corresponding
> NBD export.
> 
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> ---
>  qapi/block.json     | 18 ++++++++++++++++++
>  include/block/nbd.h |  3 ++-
>  blockdev-nbd.c      | 29 ++++++++++++++++++++++++++++-
>  nbd/server.c        | 25 ++++++++++++++++++++++---
>  4 files changed, 70 insertions(+), 5 deletions(-)
> 
> diff --git a/qapi/block.json b/qapi/block.json
> index 503d4b287b..f83485c325 100644
> --- a/qapi/block.json
> +++ b/qapi/block.json
> @@ -228,6 +228,24 @@
>    'data': {'device': 'str', '*name': 'str', '*writable': 'bool'} }
>  
>  ##
> +# @nbd-server-remove:
> +#
> +# Remove NBD export by name.
> +#
> +# @name: Export name.
> +#
> +# @force: Whether active connections to the export should be closed. If this
> +#         parameter is false the export only becomes hidden from clients (new
> +#         connections are impossible), and would be automatically freed after
> +#         all clients are disconnected (default false).

Unstated, but if the parameter is true, existing clients are forcefully
disconnected, possibly losing pending transactions.

Do we want a third mode in the middle, where the server starts replying
to all existing clients with ESHUTDOWN errors for all new requests
rather than abruptly disconnecting (no new I/O, but no forced disconnect
and pending in-flight transactions can still complete gracefully)?

> +#
> +# Returns: error if the server is not running or export is not found.
> +#
> +# Since: 2.12
> +##
> +{ 'command': 'nbd-server-remove', 'data': {'name': 'str', '*force': 'bool'} }
> +

If we're okay with just the bool parameter, then this patch looks good;
but if we want a third mode, then we want '*force' to be an enum type.
So tentative:

Reviewed-by: Eric Blake <eblake@redhat.com>
Vladimir Sementsov-Ogievskiy Jan. 12, 2018, 9:47 a.m. | #2
09.01.2018 22:52, Eric Blake wrote:
> On 12/07/2017 09:50 AM, Vladimir Sementsov-Ogievskiy wrote:
>> Add command for export removing. It is needed for cases when we
>> don't want to keep export after the operation on it was completed.
>> The other example is temporary node, created with blockdev-add.
>> If we want to delete it we should firstly remove corresponding
>> NBD export.
>>
>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
>> ---
>>   qapi/block.json     | 18 ++++++++++++++++++
>>   include/block/nbd.h |  3 ++-
>>   blockdev-nbd.c      | 29 ++++++++++++++++++++++++++++-
>>   nbd/server.c        | 25 ++++++++++++++++++++++---
>>   4 files changed, 70 insertions(+), 5 deletions(-)
>>
>> diff --git a/qapi/block.json b/qapi/block.json
>> index 503d4b287b..f83485c325 100644
>> --- a/qapi/block.json
>> +++ b/qapi/block.json
>> @@ -228,6 +228,24 @@
>>     'data': {'device': 'str', '*name': 'str', '*writable': 'bool'} }
>>   
>>   ##
>> +# @nbd-server-remove:
>> +#
>> +# Remove NBD export by name.
>> +#
>> +# @name: Export name.
>> +#
>> +# @force: Whether active connections to the export should be closed. If this
>> +#         parameter is false the export only becomes hidden from clients (new
>> +#         connections are impossible), and would be automatically freed after
>> +#         all clients are disconnected (default false).
> Unstated, but if the parameter is true, existing clients are forcefully
> disconnected, possibly losing pending transactions.
>
> Do we want a third mode in the middle, where the server starts replying
> to all existing clients with ESHUTDOWN errors for all new requests
> rather than abruptly disconnecting (no new I/O, but no forced disconnect
> and pending in-flight transactions can still complete gracefully)?

looks interesting. what about the following naming?

@mode: possible values:
               hide - just hide server from new clients, maintain 
existing connections,
                           remove after all clients disconnected
               soft - like hide, but answer with ESHUTDOWN for all 
further requests from
                           existing connections
               hard - hard disconnect all clients and remove server
               (default: soft)

new corresponding states of nbd export:
hidden, shutting_down

and we allow transitions:

normal_execution -> hidden
normal_execution -> shutting_down
normal_execution -> exit
hidden -> shutting_down
hidden -> exit
shutting_down -> exit


>
>> +#
>> +# Returns: error if the server is not running or export is not found.
>> +#
>> +# Since: 2.12
>> +##
>> +{ 'command': 'nbd-server-remove', 'data': {'name': 'str', '*force': 'bool'} }
>> +
> If we're okay with just the bool parameter, then this patch looks good;
> but if we want a third mode, then we want '*force' to be an enum type.
> So tentative:
>
> Reviewed-by: Eric Blake <eblake@redhat.com>
>
Eric Blake Jan. 15, 2018, 3:09 p.m. | #3
On 01/12/2018 03:47 AM, Vladimir Sementsov-Ogievskiy wrote:
> 09.01.2018 22:52, Eric Blake wrote:
>> On 12/07/2017 09:50 AM, Vladimir Sementsov-Ogievskiy wrote:
>>> Add command for export removing. It is needed for cases when we
>>> don't want to keep export after the operation on it was completed.
>>> The other example is temporary node, created with blockdev-add.
>>> If we want to delete it we should firstly remove corresponding
>>> NBD export.
>>>
>>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
>>> ---

>> Do we want a third mode in the middle, where the server starts replying
>> to all existing clients with ESHUTDOWN errors for all new requests
>> rather than abruptly disconnecting (no new I/O, but no forced disconnect
>> and pending in-flight transactions can still complete gracefully)?
> 
> looks interesting. what about the following naming?
> 
> @mode: possible values:
>               hide - just hide server from new clients, maintain
> existing connections,
>                           remove after all clients disconnected
>               soft - like hide, but answer with ESHUTDOWN for all
> further requests from
>                           existing connections
>               hard - hard disconnect all clients and remove server
>               (default: soft)

Or even a fourth mode that causes an immediate error return without
state change if there are any connected clients, but otherwise removes
the server.

> 
> new corresponding states of nbd export:
> hidden, shutting_down
> 
> and we allow transitions:
> 
> normal_execution -> hidden
> normal_execution -> shutting_down
> normal_execution -> exit
> hidden -> shutting_down
> hidden -> exit
> shutting_down -> exit

Seems reasonable.  Are you planning on tackling a respin of this series
incorporating that idea?
Vladimir Sementsov-Ogievskiy Jan. 15, 2018, 5:47 p.m. | #4
15.01.2018 18:09, Eric Blake wrote:
> On 01/12/2018 03:47 AM, Vladimir Sementsov-Ogievskiy wrote:
>> 09.01.2018 22:52, Eric Blake wrote:
>>> On 12/07/2017 09:50 AM, Vladimir Sementsov-Ogievskiy wrote:
>>>> Add command for export removing. It is needed for cases when we
>>>> don't want to keep export after the operation on it was completed.
>>>> The other example is temporary node, created with blockdev-add.
>>>> If we want to delete it we should firstly remove corresponding
>>>> NBD export.
>>>>
>>>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
>>>> ---
>>> Do we want a third mode in the middle, where the server starts replying
>>> to all existing clients with ESHUTDOWN errors for all new requests
>>> rather than abruptly disconnecting (no new I/O, but no forced disconnect
>>> and pending in-flight transactions can still complete gracefully)?
>> looks interesting. what about the following naming?
>>
>> @mode: possible values:
>>                hide - just hide server from new clients, maintain
>> existing connections,
>>                            remove after all clients disconnected
>>                soft - like hide, but answer with ESHUTDOWN for all
>> further requests from
>>                            existing connections
>>                hard - hard disconnect all clients and remove server
>>                (default: soft)
> Or even a fourth mode that causes an immediate error return without
> state change if there are any connected clients, but otherwise removes
> the server.
>
>> new corresponding states of nbd export:
>> hidden, shutting_down
>>
>> and we allow transitions:
>>
>> normal_execution -> hidden
>> normal_execution -> shutting_down
>> normal_execution -> exit
>> hidden -> shutting_down
>> hidden -> exit
>> shutting_down -> exit
> Seems reasonable.  Are you planning on tackling a respin of this series
> incorporating that idea?
>

yes, will do.
Vladimir Sementsov-Ogievskiy Jan. 17, 2018, 1:36 p.m. | #5
15.01.2018 20:47, Vladimir Sementsov-Ogievskiy wrote:
> 15.01.2018 18:09, Eric Blake wrote:
>> On 01/12/2018 03:47 AM, Vladimir Sementsov-Ogievskiy wrote:
>>> 09.01.2018 22:52, Eric Blake wrote:
>>>> On 12/07/2017 09:50 AM, Vladimir Sementsov-Ogievskiy wrote:
>>>>> Add command for export removing. It is needed for cases when we
>>>>> don't want to keep export after the operation on it was completed.
>>>>> The other example is temporary node, created with blockdev-add.
>>>>> If we want to delete it we should firstly remove corresponding
>>>>> NBD export.
>>>>>
>>>>> Signed-off-by: Vladimir Sementsov-Ogievskiy 
>>>>> <vsementsov@virtuozzo.com>
>>>>> ---
>>>> Do we want a third mode in the middle, where the server starts 
>>>> replying
>>>> to all existing clients with ESHUTDOWN errors for all new requests
>>>> rather than abruptly disconnecting (no new I/O, but no forced 
>>>> disconnect
>>>> and pending in-flight transactions can still complete gracefully)?
>>> looks interesting. what about the following naming?
>>>
>>> @mode: possible values:
>>>                hide - just hide server from new clients, maintain
>>> existing connections,
>>>                            remove after all clients disconnected
>>>                soft - like hide, but answer with ESHUTDOWN for all
>>> further requests from
>>>                            existing connections
>>>                hard - hard disconnect all clients and remove server
>>>                (default: soft)
>> Or even a fourth mode that causes an immediate error return without
>> state change if there are any connected clients, but otherwise removes
>> the server.
>>
>>> new corresponding states of nbd export:
>>> hidden, shutting_down
>>>
>>> and we allow transitions:
>>>
>>> normal_execution -> hidden
>>> normal_execution -> shutting_down
>>> normal_execution -> exit
>>> hidden -> shutting_down
>>> hidden -> exit
>>> shutting_down -> exit
>> Seems reasonable.  Are you planning on tackling a respin of this series
>> incorporating that idea?
>>
>
> yes, will do.
>
>

Discussed with Nikolay.
For now we actually need only one mode: hard.
In near future we _may be_ will need your proposed fourth mode (what 
about "safe" name for it ?)

I was going to implement all 4 modes, but now I doubt, isn't it too 
hastily, to introduce 3 new modes to the
interface, which we (personally) do not need. May be it is better to 
start from one or two modes.

Finally what do you think, Eric? Which modes do you need?

ps: I've created hmp version for 2/6, it will be in v2.
  also, I'm going to add query-nbd-server, which should list all exports

also, about HMP: If I understand correctly, people use it because 
writing qmp command by hand is not very comfortable.
I have a script (for managing libvirt guest, but it can be adopted for 
qemu or even used for qemu monitor), which allows
me run qmp commands on vms as easy as:

|qmp VMNAME query-block-jobs or qmp VMNAME nbd-server-remove name exp1 
mode hard or even |

|qmp VMNAME blockdev-add id disk driver qcow2 cache {writeback true 
direct true} aio native discard unmap file {driver file filename 
/tmp/somedisk} |||
Eric Blake Jan. 17, 2018, 3:23 p.m. | #6
On 01/17/2018 07:36 AM, Vladimir Sementsov-Ogievskiy wrote:

>>>> looks interesting. what about the following naming?
>>>>
>>>> @mode: possible values:
>>>>                hide - just hide server from new clients, maintain
>>>> existing connections,
>>>>                            remove after all clients disconnected
>>>>                soft - like hide, but answer with ESHUTDOWN for all
>>>> further requests from
>>>>                            existing connections
>>>>                hard - hard disconnect all clients and remove server
>>>>                (default: soft)
>>> Or even a fourth mode that causes an immediate error return without
>>> state change if there are any connected clients, but otherwise removes
>>> the server.
>>>
>>>> new corresponding states of nbd export:
>>>> hidden, shutting_down
>>>>
>>>> and we allow transitions:
>>>>
>>>> normal_execution -> hidden
>>>> normal_execution -> shutting_down
>>>> normal_execution -> exit
>>>> hidden -> shutting_down
>>>> hidden -> exit
>>>> shutting_down -> exit
>>> Seems reasonable.  Are you planning on tackling a respin of this series
>>> incorporating that idea?
>>>
>>
>> yes, will do.
>>
>>
> 
> Discussed with Nikolay.
> For now we actually need only one mode: hard.
> In near future we _may be_ will need your proposed fourth mode (what
> about "safe" name for it ?)

'safe' sounds reasonable.

Of course, if we only have two modes at front ('safe' which returns an
error if a client is connected, and 'hard' which disconnects all clients
immediately; leaving 'hide' and 'soft' for the future), then we don't
have to worry about a state transition or any hidden exports.

A QAPI enum with only two values now is at least extensible in the
future if someone has a need for another mode, and introspectible to
learn which modes are currently supported.

> 
> I was going to implement all 4 modes, but now I doubt, isn't it too
> hastily, to introduce 3 new modes to the
> interface, which we (personally) do not need. May be it is better to
> start from one or two modes.

Starting with just two modes is fine as well.

> 
> Finally what do you think, Eric? Which modes do you need?

'hide' may be interesting for the purpose of connecting a single client,
then hiding the export so no other clients can connect, while waiting
for the first client to take its time.  But right now, I don't have
actual use cases in mind so much as making sure we aren't limiting
ourself from future expansion as needs are identified, so a conservative
choice of just 'safe' and 'hard' for now is reasonable.

> 
> ps: I've created hmp version for 2/6, it will be in v2.
>  also, I'm going to add query-nbd-server, which should list all exports

Sounds good.

> 
> also, about HMP: If I understand correctly, people use it because
> writing qmp command by hand is not very comfortable.
> I have a script (for managing libvirt guest, but it can be adopted for
> qemu or even used for qemu monitor), which allows
> me run qmp commands on vms as easy as:
> 
> |qmp VMNAME query-block-jobs or qmp VMNAME nbd-server-remove name exp1
> mode hard or even |
> 
> |qmp VMNAME blockdev-add id disk driver qcow2 cache {writeback true
> direct true} aio native discard unmap file {driver file filename
> /tmp/somedisk} |||

Yeah, there are various scripting solutions around QMP that can make it
easier; but HMP is often still an easy front-line interface for experiments.
Vladimir Sementsov-Ogievskiy Jan. 17, 2018, 3:51 p.m. | #7
17.01.2018 18:23, Eric Blake wrote:
> On 01/17/2018 07:36 AM, Vladimir Sementsov-Ogievskiy wrote:
>
>>>>> looks interesting. what about the following naming?
>>>>>
>>>>> @mode: possible values:
>>>>>                 hide - just hide server from new clients, maintain
>>>>> existing connections,
>>>>>                             remove after all clients disconnected
>>>>>                 soft - like hide, but answer with ESHUTDOWN for all
>>>>> further requests from
>>>>>                             existing connections
>>>>>                 hard - hard disconnect all clients and remove server
>>>>>                 (default: soft)
>>>> Or even a fourth mode that causes an immediate error return without
>>>> state change if there are any connected clients, but otherwise removes
>>>> the server.
>>>>
>>>>> new corresponding states of nbd export:
>>>>> hidden, shutting_down
>>>>>
>>>>> and we allow transitions:
>>>>>
>>>>> normal_execution -> hidden
>>>>> normal_execution -> shutting_down
>>>>> normal_execution -> exit
>>>>> hidden -> shutting_down
>>>>> hidden -> exit
>>>>> shutting_down -> exit
>>>> Seems reasonable.  Are you planning on tackling a respin of this series
>>>> incorporating that idea?
>>>>
>>> yes, will do.
>>>
>>>
>> Discussed with Nikolay.
>> For now we actually need only one mode: hard.
>> In near future we _may be_ will need your proposed fourth mode (what
>> about "safe" name for it ?)
> 'safe' sounds reasonable.
>
> Of course, if we only have two modes at front ('safe' which returns an
> error if a client is connected, and 'hard' which disconnects all clients
> immediately; leaving 'hide' and 'soft' for the future), then we don't
> have to worry about a state transition or any hidden exports.
>
> A QAPI enum with only two values now is at least extensible in the
> future if someone has a need for another mode, and introspectible to
> learn which modes are currently supported.
>
>> I was going to implement all 4 modes, but now I doubt, isn't it too
>> hastily, to introduce 3 new modes to the
>> interface, which we (personally) do not need. May be it is better to
>> start from one or two modes.
> Starting with just two modes is fine as well.
>
>> Finally what do you think, Eric? Which modes do you need?
> 'hide' may be interesting for the purpose of connecting a single client,
> then hiding the export so no other clients can connect, while waiting
> for the first client to take its time.  But right now, I don't have
> actual use cases in mind so much as making sure we aren't limiting
> ourself from future expansion as needs are identified, so a conservative
> choice of just 'safe' and 'hard' for now is reasonable.

ok, I agree. 'safe' looks like better option for default behavior. So 
I'll post these two options in v3.

>
>> ps: I've created hmp version for 2/6, it will be in v2.
>>   also, I'm going to add query-nbd-server, which should list all exports
> Sounds good.
>
>> also, about HMP: If I understand correctly, people use it because
>> writing qmp command by hand is not very comfortable.
>> I have a script (for managing libvirt guest, but it can be adopted for
>> qemu or even used for qemu monitor), which allows
>> me run qmp commands on vms as easy as:
>>
>> |qmp VMNAME query-block-jobs or qmp VMNAME nbd-server-remove name exp1
>> mode hard or even |
>>
>> |qmp VMNAME blockdev-add id disk driver qcow2 cache {writeback true
>> direct true} aio native discard unmap file {driver file filename
>> /tmp/somedisk} |||
> Yeah, there are various scripting solutions around QMP that can make it
> easier; but HMP is often still an easy front-line interface for experiments.
>

isn't it because these solutions are not available directly in monitor, 
when HMP is?
may be, we need third type of monitor HQMP which is QMP with simplified 
syntax? Or
allow qmp commands in simplified syntax directly in HMP?
Eric Blake Jan. 17, 2018, 4:03 p.m. | #8
On 01/17/2018 09:51 AM, Vladimir Sementsov-Ogievskiy wrote:

>>> I have a script (for managing libvirt guest, but it can be adopted for
>>> qemu or even used for qemu monitor), which allows
>>> me run qmp commands on vms as easy as:
>>>
>>> |qmp VMNAME query-block-jobs or qmp VMNAME nbd-server-remove name exp1
>>> mode hard or even |
>>>
>>> |qmp VMNAME blockdev-add id disk driver qcow2 cache {writeback true
>>> direct true} aio native discard unmap file {driver file filename
>>> /tmp/somedisk} |||
>> Yeah, there are various scripting solutions around QMP that can make it
>> easier; but HMP is often still an easy front-line interface for
>> experiments.
>>
> 
> isn't it because these solutions are not available directly in monitor,
> when HMP is?

QMP can be directly accessed in a monitor; it just requires more typing.
 If you are developing QMP commands, it may be easier to use
./scripts/qmp/qmp-shell (couple it with a readline wrapper, and you can
even get tab-completion and history across sessions).  There's also
things like libvirt's 'virsh qmp-monitor-command' for shell-scripting
access to arbitrary QMP commands, provided your guest is run by libvirt.

> may be, we need third type of monitor HQMP which is QMP with simplified
> syntax? Or
> allow qmp commands in simplified syntax directly in HMP?

No, I don't think we need either thing.  Wrappers around existing
monitors is better than bloating qemu proper with a third flavor of
monitor.  And HMP is for humans, with no restrictions on back-compat
changes, so if it doesn't do something you want for quick-and-dirty
testing, you can either add a new HMP command, or just use QMP (or one
of its wrappers, like qmp-shell) in the first place.  Ultimately, our
long-term concern is only about the QMP interface; HMP is supposed to be
convenient.  So if it starts costing too much time to port a QMP
interface to HMP, then don't worry about it.
Vladimir Sementsov-Ogievskiy Jan. 17, 2018, 4:39 p.m. | #9
17.01.2018 19:03, Eric Blake wrote:
> On 01/17/2018 09:51 AM, Vladimir Sementsov-Ogievskiy wrote:
>
>>>> I have a script (for managing libvirt guest, but it can be adopted for
>>>> qemu or even used for qemu monitor), which allows
>>>> me run qmp commands on vms as easy as:
>>>>
>>>> |qmp VMNAME query-block-jobs or qmp VMNAME nbd-server-remove name exp1
>>>> mode hard or even |
>>>>
>>>> |qmp VMNAME blockdev-add id disk driver qcow2 cache {writeback true
>>>> direct true} aio native discard unmap file {driver file filename
>>>> /tmp/somedisk} |||
>>> Yeah, there are various scripting solutions around QMP that can make it
>>> easier; but HMP is often still an easy front-line interface for
>>> experiments.
>>>
>> isn't it because these solutions are not available directly in monitor,
>> when HMP is?
> QMP can be directly accessed in a monitor; it just requires more typing.
>   If you are developing QMP commands, it may be easier to use
> ./scripts/qmp/qmp-shell (couple it with a readline wrapper, and you can
> even get tab-completion and history across sessions).  There's also
> things like libvirt's 'virsh qmp-monitor-command' for shell-scripting
> access to arbitrary QMP commands, provided your guest is run by libvirt.
>
>> may be, we need third type of monitor HQMP which is QMP with simplified
>> syntax? Or
>> allow qmp commands in simplified syntax directly in HMP?
> No, I don't think we need either thing.  Wrappers around existing
> monitors is better than bloating qemu proper with a third flavor of
> monitor.  And HMP is for humans, with no restrictions on back-compat
> changes, so if it doesn't do something you want for quick-and-dirty
> testing, you can either add a new HMP command, or just use QMP (or one
> of its wrappers, like qmp-shell) in the first place.  Ultimately, our
> long-term concern is only about the QMP interface; HMP is supposed to be
> convenient.  So if it starts costing too much time to port a QMP
> interface to HMP, then don't worry about it.
>

most of commands, ported to hmp are done in same style: they just call 
corresponding qmp command.
Isn't it better to provide common interface for calling qmp commands 
through HMP monitor, to never
create hmp versions of new commands? they will be available automatically.
Dr. David Alan Gilbert Jan. 26, 2018, 3:05 p.m. | #10
* Vladimir Sementsov-Ogievskiy (vsementsov@virtuozzo.com) wrote:
> 17.01.2018 19:03, Eric Blake wrote:
> > On 01/17/2018 09:51 AM, Vladimir Sementsov-Ogievskiy wrote:
> > 
> > > > > I have a script (for managing libvirt guest, but it can be adopted for
> > > > > qemu or even used for qemu monitor), which allows
> > > > > me run qmp commands on vms as easy as:
> > > > > 
> > > > > |qmp VMNAME query-block-jobs or qmp VMNAME nbd-server-remove name exp1
> > > > > mode hard or even |
> > > > > 
> > > > > |qmp VMNAME blockdev-add id disk driver qcow2 cache {writeback true
> > > > > direct true} aio native discard unmap file {driver file filename
> > > > > /tmp/somedisk} |||
> > > > Yeah, there are various scripting solutions around QMP that can make it
> > > > easier; but HMP is often still an easy front-line interface for
> > > > experiments.
> > > > 
> > > isn't it because these solutions are not available directly in monitor,
> > > when HMP is?
> > QMP can be directly accessed in a monitor; it just requires more typing.
> >   If you are developing QMP commands, it may be easier to use
> > ./scripts/qmp/qmp-shell (couple it with a readline wrapper, and you can
> > even get tab-completion and history across sessions).  There's also
> > things like libvirt's 'virsh qmp-monitor-command' for shell-scripting
> > access to arbitrary QMP commands, provided your guest is run by libvirt.
> > 
> > > may be, we need third type of monitor HQMP which is QMP with simplified
> > > syntax? Or
> > > allow qmp commands in simplified syntax directly in HMP?
> > No, I don't think we need either thing.  Wrappers around existing
> > monitors is better than bloating qemu proper with a third flavor of
> > monitor.  And HMP is for humans, with no restrictions on back-compat
> > changes, so if it doesn't do something you want for quick-and-dirty
> > testing, you can either add a new HMP command, or just use QMP (or one
> > of its wrappers, like qmp-shell) in the first place.  Ultimately, our
> > long-term concern is only about the QMP interface; HMP is supposed to be
> > convenient.  So if it starts costing too much time to port a QMP
> > interface to HMP, then don't worry about it.
> > 
> 
> most of commands, ported to hmp are done in same style: they just call
> corresponding qmp command.
> Isn't it better to provide common interface for calling qmp commands through
> HMP monitor, to never
> create hmp versions of new commands? they will be available automatically.

It would be nice to do that, but they're not that consistent in how they
convert parameters and options, but I occasionally wonder if we could
automate more of it.

Dave

> -- 
> Best regards,
> Vladimir
> 
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
Vladimir Sementsov-Ogievskiy Feb. 6, 2018, 3:29 p.m. | #11
26.01.2018 18:05, Dr. David Alan Gilbert wrote:
> * Vladimir Sementsov-Ogievskiy (vsementsov@virtuozzo.com) wrote:
>> 17.01.2018 19:03, Eric Blake wrote:
>>> On 01/17/2018 09:51 AM, Vladimir Sementsov-Ogievskiy wrote:
>>>
>>>>>> I have a script (for managing libvirt guest, but it can be adopted for
>>>>>> qemu or even used for qemu monitor), which allows
>>>>>> me run qmp commands on vms as easy as:
>>>>>>
>>>>>> |qmp VMNAME query-block-jobs or qmp VMNAME nbd-server-remove name exp1
>>>>>> mode hard or even |
>>>>>>
>>>>>> |qmp VMNAME blockdev-add id disk driver qcow2 cache {writeback true
>>>>>> direct true} aio native discard unmap file {driver file filename
>>>>>> /tmp/somedisk} |||
>>>>> Yeah, there are various scripting solutions around QMP that can make it
>>>>> easier; but HMP is often still an easy front-line interface for
>>>>> experiments.
>>>>>
>>>> isn't it because these solutions are not available directly in monitor,
>>>> when HMP is?
>>> QMP can be directly accessed in a monitor; it just requires more typing.
>>>    If you are developing QMP commands, it may be easier to use
>>> ./scripts/qmp/qmp-shell (couple it with a readline wrapper, and you can
>>> even get tab-completion and history across sessions).  There's also
>>> things like libvirt's 'virsh qmp-monitor-command' for shell-scripting
>>> access to arbitrary QMP commands, provided your guest is run by libvirt.
>>>
>>>> may be, we need third type of monitor HQMP which is QMP with simplified
>>>> syntax? Or
>>>> allow qmp commands in simplified syntax directly in HMP?
>>> No, I don't think we need either thing.  Wrappers around existing
>>> monitors is better than bloating qemu proper with a third flavor of
>>> monitor.  And HMP is for humans, with no restrictions on back-compat
>>> changes, so if it doesn't do something you want for quick-and-dirty
>>> testing, you can either add a new HMP command, or just use QMP (or one
>>> of its wrappers, like qmp-shell) in the first place.  Ultimately, our
>>> long-term concern is only about the QMP interface; HMP is supposed to be
>>> convenient.  So if it starts costing too much time to port a QMP
>>> interface to HMP, then don't worry about it.
>>>
>> most of commands, ported to hmp are done in same style: they just call
>> corresponding qmp command.
>> Isn't it better to provide common interface for calling qmp commands through
>> HMP monitor, to never
>> create hmp versions of new commands? they will be available automatically.
> It would be nice to do that, but they're not that consistent in how they
> convert parameters and options, but I occasionally wonder if we could
> automate more of it.


What about allowing some new syntax in hmp, directly mapped to qmp?

something like

 >>> blockdev-add id disk driver qcow2 cache {writeback true direct 
true} aio native discard unmap file {driver file filename /tmp/somedisk}

?

Or it may be realized as a separate hmp command "qmp" (looks more safe 
as a first step, however, I think previous variant (direct call) is better):

 >>> qmp blockdev-add id disk driver qcow2 cache {writeback true direct 
true} aio native discard unmap file {driver file filename /tmp/somedisk}

what do think? This looks simple to implement and should be useful.

>
> Dave
>
>> -- 
>> Best regards,
>> Vladimir
>>
> --
> Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
Eric Blake Feb. 6, 2018, 4:06 p.m. | #12
On 02/06/2018 09:29 AM, Vladimir Sementsov-Ogievskiy wrote:

>>> most of commands, ported to hmp are done in same style: they just call
>>> corresponding qmp command.
>>> Isn't it better to provide common interface for calling qmp commands 
>>> through
>>> HMP monitor, to never
>>> create hmp versions of new commands? they will be available 
>>> automatically.
>> It would be nice to do that, but they're not that consistent in how they
>> convert parameters and options, but I occasionally wonder if we could
>> automate more of it.
> 
> 
> What about allowing some new syntax in hmp, directly mapped to qmp?
> 
> something like
> 
>  >>> blockdev-add id disk driver qcow2 cache {writeback true direct 
> true} aio native discard unmap file {driver file filename /tmp/somedisk}
> 

Personally, if I'm testing blockdev-add, I'll use QMP directly (or even 
scripts/qmp/qmp-shell or virsh qemu-monitor-command), not an HMP wrapper 
where I have to learn a new syntax of how to write something that will 
convert to QMP.  We already have enough different ways to write things 
that I don't need to learn yet another syntax wrapper.  Or maybe what 
I'm saying is that instead of inventing a new syntax, that if you DO add 
an HMP command that forwards to QMP, please reuse an existing syntax 
(whether direct JSON as used by QMP, or the syntax used by qmp-shell).

If you think writing a new HMP command is worth it, I won't stop you 
from writing it.  But at this point, our current approach of writing a 
manual wrapper per command as we have interest, rather than a generic 
wrap-anything, has worked for the cases that HMP users have cared about. 
  Remember, QMP is the interface that MUST work, while HMP is only for 
convenience, and if it is not trivial to make HMP do everything that QMP 
can do, it is no real loss.

> ?
> 
> Or it may be realized as a separate hmp command "qmp" (looks more safe 
> as a first step, however, I think previous variant (direct call) is 
> better):
> 
>  >>> qmp blockdev-add id disk driver qcow2 cache {writeback true direct 
> true} aio native discard unmap file {driver file filename /tmp/somedisk}
> 
> what do think? This looks simple to implement and should be useful.

Up to you if you want to tackle anything like that, but it would be a 
new thread (a generic way to invoke QMP from HMP is independent of 
nbd-server-remove).
Vladimir Sementsov-Ogievskiy Feb. 6, 2018, 5:54 p.m. | #13
06.02.2018 19:06, Eric Blake wrote:
> On 02/06/2018 09:29 AM, Vladimir Sementsov-Ogievskiy wrote:
>
>>>> most of commands, ported to hmp are done in same style: they just call
>>>> corresponding qmp command.
>>>> Isn't it better to provide common interface for calling qmp 
>>>> commands through
>>>> HMP monitor, to never
>>>> create hmp versions of new commands? they will be available 
>>>> automatically.
>>> It would be nice to do that, but they're not that consistent in how 
>>> they
>>> convert parameters and options, but I occasionally wonder if we could
>>> automate more of it.
>>
>>
>> What about allowing some new syntax in hmp, directly mapped to qmp?
>>
>> something like
>>
>>  >>> blockdev-add id disk driver qcow2 cache {writeback true direct 
>> true} aio native discard unmap file {driver file filename /tmp/somedisk}
>>
>
> Personally, if I'm testing blockdev-add, I'll use QMP directly (or 
> even scripts/qmp/qmp-shell or virsh qemu-monitor-command), not an HMP 
> wrapper where I have to learn a new syntax of how to write something 
> that will convert to QMP.  We already have enough different ways to 
> write things that I don't need to learn yet another syntax wrapper.  
> Or maybe what I'm saying is that instead of inventing a new syntax, 
> that if you DO add an HMP command that forwards to QMP, please reuse 
> an existing syntax (whether direct JSON as used by QMP, or the syntax 
> used by qmp-shell).

I'm afraid, that JSON is too hard to use in human monitor. And this will 
make the whole feature useless.

>
> If you think writing a new HMP command is worth it, I won't stop you 
> from writing it.  But at this point, our current approach of writing a 
> manual wrapper per command as we have interest, rather than a generic 
> wrap-anything, has worked for the cases that HMP users have cared 
> about.  Remember, QMP is the interface that MUST work, while HMP is 
> only for convenience, and if it is not trivial to make HMP do 
> everything that QMP can do, it is no real loss.
>

But we create hmp wrappers on demand, and for each case, we actually 
invent new syntax. I just search for the way to avoid creating new and 
new hmp wrappers, by introducing new syntax only once.
And, here is almost nothing to learn:

command := command-name parameters
parameters = [key value ]...
value = simple-value | array | map
map = '{' parameters '}'
array = '[' [value ]... ']'

another variant is to use yaml - like json, but we do not need put all 
keys into quotes.

On the other hand, implementing new parser in qemu is not trivial task 
(hmm, I don't want do it=), it should be simpler to create direct JSON 
wrapper in HMP monitor, and use some python wrapper around the monitor. 
And this looks useless, as with same result I can use wrapper around QMP 
monitor. So, may be the most interesting solution would be to make some 
easy-to-use python-based wrapper, which will give a simple way to use 
both qmp and hmp commands.. I'll think about it. However it doesn't 
solve initial problem of creating new and new hmp wrappers by hand.

>> ?
>>
>> Or it may be realized as a separate hmp command "qmp" (looks more 
>> safe as a first step, however, I think previous variant (direct call) 
>> is better):
>>
>>  >>> qmp blockdev-add id disk driver qcow2 cache {writeback true 
>> direct true} aio native discard unmap file {driver file filename 
>> /tmp/somedisk}
>>
>> what do think? This looks simple to implement and should be useful.
>
> Up to you if you want to tackle anything like that, but it would be a 
> new thread (a generic way to invoke QMP from HMP is independent of 
> nbd-server-remove).
>
Dr. David Alan Gilbert Feb. 6, 2018, 6:38 p.m. | #14
* Vladimir Sementsov-Ogievskiy (vsementsov@virtuozzo.com) wrote:
> 26.01.2018 18:05, Dr. David Alan Gilbert wrote:
> > * Vladimir Sementsov-Ogievskiy (vsementsov@virtuozzo.com) wrote:
> > > 17.01.2018 19:03, Eric Blake wrote:
> > > > On 01/17/2018 09:51 AM, Vladimir Sementsov-Ogievskiy wrote:
> > > > 
> > > > > > > I have a script (for managing libvirt guest, but it can be adopted for
> > > > > > > qemu or even used for qemu monitor), which allows
> > > > > > > me run qmp commands on vms as easy as:
> > > > > > > 
> > > > > > > |qmp VMNAME query-block-jobs or qmp VMNAME nbd-server-remove name exp1
> > > > > > > mode hard or even |
> > > > > > > 
> > > > > > > |qmp VMNAME blockdev-add id disk driver qcow2 cache {writeback true
> > > > > > > direct true} aio native discard unmap file {driver file filename
> > > > > > > /tmp/somedisk} |||
> > > > > > Yeah, there are various scripting solutions around QMP that can make it
> > > > > > easier; but HMP is often still an easy front-line interface for
> > > > > > experiments.
> > > > > > 
> > > > > isn't it because these solutions are not available directly in monitor,
> > > > > when HMP is?
> > > > QMP can be directly accessed in a monitor; it just requires more typing.
> > > >    If you are developing QMP commands, it may be easier to use
> > > > ./scripts/qmp/qmp-shell (couple it with a readline wrapper, and you can
> > > > even get tab-completion and history across sessions).  There's also
> > > > things like libvirt's 'virsh qmp-monitor-command' for shell-scripting
> > > > access to arbitrary QMP commands, provided your guest is run by libvirt.
> > > > 
> > > > > may be, we need third type of monitor HQMP which is QMP with simplified
> > > > > syntax? Or
> > > > > allow qmp commands in simplified syntax directly in HMP?
> > > > No, I don't think we need either thing.  Wrappers around existing
> > > > monitors is better than bloating qemu proper with a third flavor of
> > > > monitor.  And HMP is for humans, with no restrictions on back-compat
> > > > changes, so if it doesn't do something you want for quick-and-dirty
> > > > testing, you can either add a new HMP command, or just use QMP (or one
> > > > of its wrappers, like qmp-shell) in the first place.  Ultimately, our
> > > > long-term concern is only about the QMP interface; HMP is supposed to be
> > > > convenient.  So if it starts costing too much time to port a QMP
> > > > interface to HMP, then don't worry about it.
> > > > 
> > > most of commands, ported to hmp are done in same style: they just call
> > > corresponding qmp command.
> > > Isn't it better to provide common interface for calling qmp commands through
> > > HMP monitor, to never
> > > create hmp versions of new commands? they will be available automatically.
> > It would be nice to do that, but they're not that consistent in how they
> > convert parameters and options, but I occasionally wonder if we could
> > automate more of it.
> 
> 
> What about allowing some new syntax in hmp, directly mapped to qmp?
> 
> something like
> 
> >>> blockdev-add id disk driver qcow2 cache {writeback true direct true} aio
> native discard unmap file {driver file filename /tmp/somedisk}
> 
> ?

Hmm, I don't particularly find that easy to read either; however the
actual block device specification for HMP should be the same as what we
pass on the command line, so we only have to worry about any extra
things that are part of blockdev_add.
(I'm sure we can find a way of making the one we pass on the commandline
more readable as well, there's so much duplication).

Dave

> Or it may be realized as a separate hmp command "qmp" (looks more safe as a
> first step, however, I think previous variant (direct call) is better):
> 
> >>> qmp blockdev-add id disk driver qcow2 cache {writeback true direct true}
> aio native discard unmap file {driver file filename /tmp/somedisk}
> 
> what do think? This looks simple to implement and should be useful.
> 
> > 
> > Dave
> > 
> > > -- 
> > > Best regards,
> > > Vladimir
> > > 
> > --
> > Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
> 
> 
> -- 
> Best regards,
> Vladimir
> 
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
Markus Armbruster Feb. 7, 2018, 7:14 a.m. | #15
"Dr. David Alan Gilbert" <dgilbert@redhat.com> writes:

> * Vladimir Sementsov-Ogievskiy (vsementsov@virtuozzo.com) wrote:
>> 26.01.2018 18:05, Dr. David Alan Gilbert wrote:
>> > * Vladimir Sementsov-Ogievskiy (vsementsov@virtuozzo.com) wrote:
[...]
>> > > most of commands, ported to hmp are done in same style: they just call
>> > > corresponding qmp command.

HMP commands *should* call the QMP command to do the actual work.  That
way, we *know* all the functionality is available in QMP, and HMP is
consistent with it.

Sometimes, calling helpers shared with QMP is more convenient, and
that's okay, but then you have to think about QMP completeness and
HMP/QMP consistency.

The only exception are HMP commands that don't make sense in QMP, such
as @cpu.

>> > > Isn't it better to provide common interface for calling qmp commands through
>> > > HMP monitor, to never
>> > > create hmp versions of new commands? they will be available automatically.
>> > It would be nice to do that, but they're not that consistent in how they
>> > convert parameters and options, but I occasionally wonder if we could
>> > automate more of it.
>> 
>> 
>> What about allowing some new syntax in hmp, directly mapped to qmp?
>> 
>> something like
>> 
>> >>> blockdev-add id disk driver qcow2 cache {writeback true direct true} aio
>> native discard unmap file {driver file filename /tmp/somedisk}
>> 
>> ?
>
> Hmm, I don't particularly find that easy to read either; however the
> actual block device specification for HMP should be the same as what we
> pass on the command line, so we only have to worry about any extra
> things that are part of blockdev_add.
> (I'm sure we can find a way of making the one we pass on the commandline
> more readable as well, there's so much duplication).

Good points.

QMP syntax is different for a good reason: it serves machines rather
than humans.

Both HMP and command line serve the same humans, yet the syntax they
wrap around common functionality is different.  Sad waste of developer
time, sad waste of user brain power.  The former could perhaps be
reduced with better tooling, say having QAPI generate the details.

If you have QAPI generate HMP and command line from the exact same
definitions as QMP, you get what Vladimir wants: different interfaces to
the exact same functionality, without additional coding.

Note that the needs of humans and machines differ in more ways than just
syntax.  For instance, humans appreciate convenience features to save
typing.  In a machine interface, they'd add unnecessary and
inappropriate complexity.  Adding convenience is a good reason for
actually designing the HMP interface, rather than copying the QMP one
blindly.

Patch

diff --git a/qapi/block.json b/qapi/block.json
index 503d4b287b..f83485c325 100644
--- a/qapi/block.json
+++ b/qapi/block.json
@@ -228,6 +228,24 @@ 
   'data': {'device': 'str', '*name': 'str', '*writable': 'bool'} }
 
 ##
+# @nbd-server-remove:
+#
+# Remove NBD export by name.
+#
+# @name: Export name.
+#
+# @force: Whether active connections to the export should be closed. If this
+#         parameter is false the export only becomes hidden from clients (new
+#         connections are impossible), and would be automatically freed after
+#         all clients are disconnected (default false).
+#
+# Returns: error if the server is not running or export is not found.
+#
+# Since: 2.12
+##
+{ 'command': 'nbd-server-remove', 'data': {'name': 'str', '*force': 'bool'} }
+
+##
 # @nbd-server-stop:
 #
 # Stop QEMU's embedded NBD server, and unregister all devices previously
diff --git a/include/block/nbd.h b/include/block/nbd.h
index 113c707a5e..b89d116597 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -261,12 +261,13 @@  NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
                           bool writethrough, BlockBackend *on_eject_blk,
                           Error **errp);
 void nbd_export_close(NBDExport *exp);
+void nbd_export_hide(NBDExport *exp);
 void nbd_export_get(NBDExport *exp);
 void nbd_export_put(NBDExport *exp);
 
 BlockBackend *nbd_export_get_blockdev(NBDExport *exp);
 
-NBDExport *nbd_export_find(const char *name);
+NBDExport *nbd_export_find(const char *name, bool include_hidden);
 void nbd_export_set_name(NBDExport *exp, const char *name);
 void nbd_export_set_description(NBDExport *exp, const char *description);
 void nbd_export_close_all(void);
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
index 46c885aa35..2495d38f2c 100644
--- a/blockdev-nbd.c
+++ b/blockdev-nbd.c
@@ -174,7 +174,7 @@  void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
         name = device;
     }
 
-    if (nbd_export_find(name)) {
+    if (nbd_export_find(name, true)) {
         error_setg(errp, "NBD server already has export named '%s'", name);
         return;
     }
@@ -207,6 +207,33 @@  void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
     nbd_export_put(exp);
 }
 
+void qmp_nbd_server_remove(const char *name, bool has_force, bool force,
+                           Error **errp)
+{
+    NBDExport *exp;
+
+    if (!nbd_server) {
+        error_setg(errp, "NBD server not running");
+        return;
+    }
+
+    exp = nbd_export_find(name, true);
+    if (exp == NULL) {
+        error_setg(errp, "Export '%s' is not found", name);
+        return;
+    }
+
+    if (!has_force) {
+        force = false;
+    }
+
+    if (force) {
+        nbd_export_close(exp);
+    } else {
+        nbd_export_hide(exp);
+    }
+}
+
 void qmp_nbd_server_stop(Error **errp)
 {
     nbd_export_close_all();
diff --git a/nbd/server.c b/nbd/server.c
index e817c48087..d85f2dc747 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -78,6 +78,10 @@  struct NBDExport {
 
     BlockBackend *eject_notifier_blk;
     Notifier eject_notifier;
+
+    /* hidden export is not available for new connections and should be removed
+     * after last client disconnect */
+    bool hidden;
 };
 
 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
@@ -300,7 +304,7 @@  static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length,
 
     trace_nbd_negotiate_handle_export_name_request(name);
 
-    client->exp = nbd_export_find(name);
+    client->exp = nbd_export_find(name, false);
     if (!client->exp) {
         error_setg(errp, "export not found");
         return -EINVAL;
@@ -429,7 +433,7 @@  static int nbd_negotiate_handle_info(NBDClient *client, uint32_t length,
     }
     assert(length == 0);
 
-    exp = nbd_export_find(name);
+    exp = nbd_export_find(name, false);
     if (!exp) {
         return nbd_negotiate_send_rep_err(client->ioc, NBD_REP_ERR_UNKNOWN,
                                           opt, errp, "export '%s' not present",
@@ -966,6 +970,9 @@  void nbd_client_put(NBDClient *client)
         if (client->exp) {
             QTAILQ_REMOVE(&client->exp->clients, client, next);
             nbd_export_put(client->exp);
+            if (client->exp->hidden && QTAILQ_EMPTY(&client->exp->clients)) {
+                nbd_export_close(client->exp);
+            }
         }
         g_free(client);
     }
@@ -1125,10 +1132,14 @@  fail:
     return NULL;
 }
 
-NBDExport *nbd_export_find(const char *name)
+NBDExport *nbd_export_find(const char *name, bool include_hidden)
 {
     NBDExport *exp;
     QTAILQ_FOREACH(exp, &exports, next) {
+        if (!include_hidden && exp->hidden) {
+            continue;
+        }
+
         if (strcmp(name, exp->name) == 0) {
             return exp;
         }
@@ -1177,6 +1188,14 @@  void nbd_export_close(NBDExport *exp)
     nbd_export_put(exp);
 }
 
+void nbd_export_hide(NBDExport *exp)
+{
+    exp->hidden = true;
+    if (QTAILQ_EMPTY(&exp->clients)) {
+        nbd_export_close(exp);
+    }
+}
+
 void nbd_export_get(NBDExport *exp)
 {
     assert(exp->refcount > 0);