qga: add three logical memory hotplug related commands
v2 cover qga: add three logical memory hotplug related commands
This patch series add three guest commands about memory block:
guest-get-memory-blocks, guest-set-memory-blocks, guest-get-memory-block-size.
With these three commands, we can get information about guest's memory block online/offline status and memory block size (unit of memory online/offline operation ). Also, we can change guest's memory block status (Logical memory hotplug/unplug) from host.
Example of usage:
{"execute":"guest-get-memory-blocks"} {"return":[{"online":true,"can-offline":false,"phys-index":0},{"online":true,"can-offline":true,"phys-index":1},{"online":true,"can-offline":false,"phys-index":2},{"online":true,"can-offline":false,"phys-index":3}]}
{"execute":"guest-set-memory-blocks","arguments":{"mem-blks":[{"phys-index":0,"online":false},{"phys-index":1,"online":false},{"phys-index":3,"online":false}]}} {"return":[{"response":"operation-failed","error-code":22,"phys-index":0},{"response":"success","phys-index":1},{"response":"operation-failed","error-code":16,"phys-index":3}]}
{"execute":"guest-get-memory-block-size"}
{"return":134217728}
v2:
- Change return value of 'guest-set-memory-blocks' command from 'int' to a list of 'GuestMemoryBlockResponse', which contains more info about the operation results. It is suggested by Michael Roth. It is more useful for callers to know the exact operation result for each memory block, and also they can help callers to decide what to do next according to different error-code.
zhanghailiang (5):
qga: introduce three guest memory block commmands with stubs
qga: implement qmp_guest_get_memory_blocks() for Linux with sysfs
qga: implement qmp_guest_set_memory_blocks() for Linux with sysfs
qga: implement qmp_guest_get_memory_block_size() for Linux with sysfs
qga: add memory block command that unsupported to blacklist
qga/commands-posix.c | 324 ++++++++++++++++++++++++++++++++++++++++++++++++++- qga/commands-win32.c | 21 ++++ qga/qapi-schema.json | 114 ++++++++++++++++++ 3 files changed, 458 insertions(+), 1 deletion(-)
Signature · 1 line
1.7.12.4
PATCH v2 1/5 qga: introduce three guest memory block commmands with stubs
Introduce three new guest commands:
guest-get-memory-blocks, guest-set-memory-blocks, guest-get-memory-block-size.
With these three commands, we can support online/offline guest's memory block (logical memory hotplug/unplug) as required from host.
--- qga/commands-posix.c | 38 +++++++++++++++++ qga/commands-win32.c | 19 +++++++++ qga/qapi-schema.json | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+)
qga/commands-posix.c +38 -0
@@ -1875,6 +1875,25 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) return processed; } +GuestMemoryBlockList *qmp_guest_get_memory_blocks (Error **errp) +{ +error_set (errp,QERR_UNSUPPORTED ); +return NULL ; +} + +GuestMemoryBlockResponseList * +qmp_guest_set_memory_blocks (GuestMemoryBlockList *mem_blks,Error **errp) +{ +error_set (errp,QERR_UNSUPPORTED ); +return NULL ; +} + +int64_t qmp_guest_get_memory_block_size (Error **errp) +{ +error_set (errp,QERR_UNSUPPORTED ); +return -1 ; +} +#else /* defined(__linux__) */ void qmp_guest_suspend_disk (Error **errp)
@@ -1910,6 +1929,25 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp) return -1; } +GuestMemoryBlockList *qmp_guest_get_memory_blocks (Error **errp) +{ +error_set (errp,QERR_UNSUPPORTED ); +return NULL ; +} + +GuestMemoryBlockResponseList * +qmp_guest_set_memory_blocks (GuestMemoryBlockList *mem_blks,Error **errp) +{ +error_set (errp,QERR_UNSUPPORTED ); +return NULL ; +} + +int64_t qmp_guest_get_memory_block_size (Error **errp) +{ +error_set (errp,QERR_UNSUPPORTED ); +return -1 ; +} +#endif #if !defined(CONFIG_FSFREEZE )
qga/commands-win32.c +19 -0
@@ -446,6 +446,25 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)return -1 ; } +GuestMemoryBlockList *qmp_guest_get_memory_blocks (Error **errp) +{ +error_set (errp,QERR_UNSUPPORTED ); +return NULL ; +} + +GuestMemoryBlockResponseList * +qmp_guest_set_memory_blocks (GuestMemoryBlockList *mem_blks,Error **errp) +{ +error_set (errp,QERR_UNSUPPORTED ); +return NULL ; +} + +int64_t qmp_guest_get_memory_block_size (Error **errp) +{ +error_set (errp,QERR_UNSUPPORTED ); +return -1 ; +} +/* add unsupported commands to the blacklist */ GList *ga_command_blacklist_init (GList *blacklist) {
qga/qapi-schema.json +114 -0
117 lines not on this page · raw message
Signature · 1 line
1.7.12.4
Re: [Qemu-devel] [PATCH v2 1/5] qga: introduce three guest memory block commmands with stubs
Quoted 18 lines · zhanghailiang
Introduce three new guest commands:
guest-get-memory-blocks, guest-set-memory-blocks, guest-get-memory-block-size.With these three commands, we can support online/offline guest's memory block (logical memory hotplug/unplug) as required from host.
Signed-off-by zhanghailiang <[email protected]>--- qga/commands-posix.c | 38 +++++++++++++++++ qga/commands-win32.c | 19 +++++++++ qga/qapi-schema.json | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+)qga/qapi-schema.json +4
-0+ +## +# @guest-get-memory-block-size: +#
qga/qapi-schema.json line 854
Quoted 10 lines
qga/qapi-schema.json +10
-0+# Get the the size (in bytes) of a memory block in guest. +# It is the unit of memory block online/offline operation (also called Logical +# Memory Hotplug). +# +# Returns: memory block size in bytes. +# +# Since 2.3 +## +{ 'command': 'guest-get-memory-block-size', + 'returns': 'int' }
Yuck. This is not extensible. Please consider instead providing a dictionary-based response, so that you can add further dictionary members in the future if you have additional pieces of information to return. In fact, if you do that, it might be better to name this guest-get-memory-block-info, where size is the only info you provide now, but where adding other parameters in the future is much easier to predict.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v2 2/5 qga: implement qmp_guest_get_memory_blocks() for Linux with sysfs
In reply to zhanghailiangWe can get guest's memory block information by using command "guest-get-memory-blocks", the returned value contains a list of memory block info, such as phys-index, online state, can-offline info.
--- qga/commands-posix.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 230 insertions(+), 1 deletion(-)
qga/commands-posix.c +230 -1
@@ -1875,9 +1875,238 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorList *vcpus, Error **errp)return processed; } +static void ga_read_sysfs_file (int dirfd,const char *pathname,char *buf, +int size,Error **errp) +{ +int fd; +int res; + + errno =0 ; + fd =openat (dirfd, pathname,O_RDONLY ); +if (fd ==-1 ) { +error_setg_errno (errp, errno,"open sysfs file \"%s\"" , pathname); +return ; + } + + res =pread (fd, buf, size,0 ); +if (res ==-1 ) { +error_setg_errno (errp, errno,"pread sysfs file \"%s\"" , pathname); + }else if (res ==0 ) { +error_setg (errp,"pread sysfs file \"%s\": unexpected EOF" , pathname); + } +close (fd); +} + +static void ga_write_sysfs_file (int dirfd,const char *pathname, +const char *buf,int size,Error **errp) +{ +int fd; + + errno =0 ; + fd =openat (dirfd, pathname,O_WRONLY ); +if (fd ==-1 ) { +error_setg_errno (errp, errno,"open sysfs file \"%s\"" , pathname); +return ; + } + +if (pwrite (fd, buf, size,0 ) ==-1 ) { +error_setg_errno (errp, errno,"pwrite sysfs file \"%s\"" , pathname); + } + +close (fd); +} + +/* Transfer online/offline status between @mem_blk and the guest system. +* +* On input either @errp or *@errp must be NULL. +* +* In system-to-@mem_blk direction, the following @mem_blk fields are accessed: +* - R: mem_blk->phys_index +* - W: mem_blk->online +* - W: mem_blk->can_offline +* +* In @mem_blk-to-system direction, the following @mem_blk fields are accessed: +* - R: mem_blk->phys_index +* - R: mem_blk->online +*- R: mem_blk->can_offline +* Written members remain unmodified on error. +*/ +static void transfer_memory_block (GuestMemoryBlock *mem_blk,bool sys2memblk, +GuestMemoryBlockResponse *result, +Error **errp) +{ +char *dirpath; +int dirfd; +char *status; +Error *local_err =NULL ; + +if (!sys2memblk) { +DIR *dp; + +if (!result) { +error_setg (errp,"Internal error, 'result' should not be NULL" ); +return ; + } + errno =0 ; + dp =opendir ("/sys/devices/system/memory/" ); +/* if there is no 'memory' directory in sysfs, +* we think this VM does not support online/offline memory block, +* any other solution? +*/ +if (!dp && errno ==ENOENT ) { + result->response = +GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED ; + goto out1; + } +closedir (dp); + } + + dirpath =g_strdup_printf ("/sys/devices/system/memory/memory%" PRId64"/" , + mem_blk->phys_index); + dirfd =open (dirpath,O_RDONLY |O_DIRECTORY ); +if (dirfd ==-1 ) { +if (sys2memblk) { +error_setg_errno (errp, errno,"open(\"%s\")" , dirpath); + }else { +if (errno ==ENOENT ) { + result->response =GUEST_MEMORY_BLOCK_RESPONSE_TYPE_NOT_FOUND ; + }else { + result->response = +GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED ; + } + } +g_free (dirpath); + goto out1; + } +g_free (dirpath); + + status =g_malloc0 (10 ); +ga_read_sysfs_file (dirfd,"state" , status,10 , &local_err); +if (local_err) { +/* treat with sysfs file that not exist in old kernel */ +if (errno ==ENOENT ) { +error_free (local_err); +if (sys2memblk) { + mem_blk->online = true; + mem_blk->can_offline = false; + }else if (!mem_blk->online) { + result->response = +GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_NOT_SUPPORTED ; + } + }else { +if (sys2memblk) { +error_propagate (errp, local_err); + }else { + result->response = +GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED ; + } + } + goto out2; + } + +if (sys2memblk) { +char removable ='0' ; + + mem_blk->online = (strncmp (status,"online" ,6 ) ==0 ); + +ga_read_sysfs_file (dirfd,"removable" , &removable,1 , &local_err); +if (local_err) { +/* if no 'removable' file, it does't support offline mem blk */ +if (errno ==ENOENT ) { +error_free (local_err); + mem_blk->can_offline = false; + }else { +error_propagate (errp, local_err); + } + }else { + mem_blk->can_offline = (removable !='0' ); + } + }else { +if (mem_blk->online != (strncmp (status,"online" ,6 ) ==0 )) { +char *new_state = mem_blk->online ?g_strdup ("online" ) : +g_strdup ("offline" ); + +ga_write_sysfs_file (dirfd,"state" , new_state,strlen (new_state), + &local_err); +g_free (new_state); +if (local_err) { +error_free (local_err); + result->response = +GUEST_MEMORY_BLOCK_RESPONSE_TYPE_OPERATION_FAILED ; + goto out2; + } + + result->response =GUEST_MEMORY_BLOCK_RESPONSE_TYPE_SUCCESS ; + result->has_error_code = false; + }/* otherwise pretend successful re-(on|off)-lining */ + } +g_free (status); +close (dirfd); +return ; + +out2: +g_free (status); +close (dirfd); +out1: +if (!sys2memblk) { + result->has_error_code = true; + result->error_code = errno; + } +} +GuestMemoryBlockList *qmp_guest_get_memory_blocks (Error **errp) { -error_set (errp,QERR_UNSUPPORTED ); +GuestMemoryBlockList *head, **link; +Error *local_err =NULL ; +struct dirent *de; +DIR *dp; + + head =NULL ; + link = &head; + + dp =opendir ("/sys/devices/system/memory/" ); +if (!dp) { +error_setg_errno (errp, errno,"Can't open directory" +"\"/sys/devices/system/memory/\"\n" ); +return NULL ; + } + +/* Note: the phys_index of memory block may be discontinuous, +* this is because a memblk is the unit of the Sparse Memory design, which +* allows discontinuous memory ranges (ex. NUMA), so here we should +* traverse the memory block directory. +*/ +while ((de =readdir (dp)) !=NULL ) { +GuestMemoryBlock *mem_blk; +GuestMemoryBlockList *entry; + +if ((strncmp (de->d_name,"memory" ,6 ) !=0 ) || + !(de->d_type &DT_DIR )) { +continue ; + } + + mem_blk =g_malloc0 (sizeof *mem_blk); +/* The d_name is "memoryXXX", phys_index is block id, same as XXX */ + mem_blk->phys_index =strtoul (&de->d_name[6 ],NULL ,10 ); + mem_blk->has_can_offline = true;/* lolspeak ftw */ +transfer_memory_block (mem_blk, true,NULL , &local_err); + + entry =g_malloc0 (sizeof *entry); + entry->value = mem_blk; + + *link = entry; + link = &entry->next; + } + +closedir (dp); +if (local_err ==NULL ) { +/* there's no guest with zero memory blocks */ +g_assert (head !=NULL ); +return head; + } + +qapi_free_GuestMemoryBlockList (head); +error_propagate (errp, local_err);return NULL ; }
Signature · 1 line
1.7.12.4
PATCH v2 3/5 qga: implement qmp_guest_set_memory_blocks() for Linux with sysfs
In reply to zhanghailiangWe can change guest's online/offline state of memory blocks, by using command 'guest-set-memory-blocks'.
--- qga/commands-posix.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
qga/commands-posix.c +29 -1
@@ -2113,7 +2113,35 @@ GuestMemoryBlockList *qmp_guest_get_memory_blocks(Error **errp)GuestMemoryBlockResponseList *qmp_guest_set_memory_blocks (GuestMemoryBlockList *mem_blks,Error **errp) { -error_set (errp,QERR_UNSUPPORTED ); +GuestMemoryBlockResponseList *head, **link; +Error *local_err =NULL ; + + head =NULL ; + link = &head; + +while (mem_blks !=NULL ) { +GuestMemoryBlockResponse *result; +GuestMemoryBlockResponseList *entry; +GuestMemoryBlock *current_mem_blk = mem_blks->value; + + result =g_malloc0 (sizeof (*result)); + result->phys_index = current_mem_blk->phys_index; +transfer_memory_block (current_mem_blk, false, result, &local_err); +if (local_err) {/* should never happen */ + goto err; + } + entry =g_malloc0 (sizeof *entry); + entry->value = result; + + *link = entry; + link = &entry->next; + mem_blks = mem_blks->next; + } + +return head; +err: +qapi_free_GuestMemoryBlockResponseList (head); +error_propagate (errp, local_err);return NULL ; }
Signature · 1 line
1.7.12.4
PATCH v2 4/5 qga: implement qmp_guest_get_memory_block_size() for Linux with sysfs
In reply to zhanghailiangThe size of a memory block is architecture dependent, it represents the logical unit upon which memory online/offline operations are to be performed.
--- qga/commands-posix.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
qga/commands-posix.c +27 -2
@@ -2147,8 +2147,33 @@ err:int64_t qmp_guest_get_memory_block_size (Error **errp) { -error_set (errp,QERR_UNSUPPORTED ); -return -1 ; +Error *local_err =NULL ; +char *dirpath; +int dirfd; +char *buf; +int64_t block_size; + + dirpath =g_strdup_printf ("/sys/devices/system/memory/" ); + dirfd =open (dirpath,O_RDONLY |O_DIRECTORY ); +if (dirfd ==-1 ) { +error_setg_errno (errp, errno,"open(\"%s\")" , dirpath); +g_free (dirpath); +return -1 ; + } +g_free (dirpath); + + buf =g_malloc0 (20 ); +ga_read_sysfs_file (dirfd,"block_size_bytes" , buf,20 , &local_err); +if (local_err) { +g_free (buf); +error_propagate (errp, local_err); +return -1 ; + } + + block_size =strtol (buf,NULL ,16 );/* the unit is bytes */ +g_free (buf); + +return block_size; }#else /* defined(__linux__) */
Signature · 1 line
1.7.12.4
PATCH v2 5/5 qga: add memory block command that unsupported to blacklist
In reply to zhanghailiangFor memory block command, we only support for linux with sysfs.
--- qga/commands-posix.c | 4 +++- qga/commands-win32.c | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-)
qga/commands-posix.c +3 -1
@@ -2286,7 +2286,9 @@ GList *ga_command_blacklist_init(GList *blacklist)const char *list[] = {"guest-suspend-disk" ,"guest-suspend-ram" ,"guest-suspend-hybrid" ,"guest-network-get-interfaces" , -"guest-get-vcpus" ,"guest-set-vcpus" ,NULL }; +"guest-get-vcpus" ,"guest-set-vcpus" , +"guest-get-memory-blocks" ,"guest-set-memory-blocks" , +"guest-get-memory-block-size" ,NULL };char **p = (char **)list;while (*p) {
qga/commands-win32.c +2 -0
@@ -473,6 +473,8 @@ GList *ga_command_blacklist_init(GList *blacklist)"guest-file-write" ,"guest-file-seek" ,"guest-file-flush" ,"guest-suspend-hybrid" ,"guest-network-get-interfaces" ,"guest-get-vcpus" ,"guest-set-vcpus" , +"guest-get-memory-blocks" ,"guest-set-memory-blocks" , +"guest-get-memory-block-size" ,"guest-fsfreeze-freeze-list" ,"guest-get-fsinfo" , "guest-fstrim", NULL};char **p = (char **)list_unsupported;
Signature · 1 line
1.7.12.4
ping...
Any comments/feedbacks are warmly welcomed.
Michael? What's your opinion? ;) Thanks.
Quoted 38 lines · zhanghailiang
This patch series add three guest commands about memory block:
guest-get-memory-blocks, guest-set-memory-blocks, guest-get-memory-block-size.With these three commands, we can get information about guest's memory block online/offline status and memory block size (unit of memory online/offline operation ). Also, we can change guest's memory block status (Logical memory hotplug/unplug) from host.
Example of usage:
{"execute":"guest-get-memory-blocks"} {"return":[{"online":true,"can-offline":false,"phys-index":0},{"online":true,"can-offline":true,"phys-index":1},{"online":true,"can-offline":false,"phys-index":2},{"online":true,"can-offline":false,"phys-index":3}]}
{"execute":"guest-set-memory-blocks","arguments":{"mem-blks":[{"phys-index":0,"online":false},{"phys-index":1,"online":false},{"phys-index":3,"online":false}]}} {"return":[{"response":"operation-failed","error-code":22,"phys-index":0},{"response":"success","phys-index":1},{"response":"operation-failed","error-code":16,"phys-index":3}]}
{"execute":"guest-get-memory-block-size"}
{"return":134217728}v2:
- Change return value of 'guest-set-memory-blocks' command from 'int' to a list of 'GuestMemoryBlockResponse', which contains more info about the operation results. It is suggested by Michael Roth. It is more useful for callers to know the exact operation result for each memory block, and also they can help callers to decide what to do next according to different error-code.zhanghailiang (5):
qga: introduce three guest memory block commmands with stubs
qga: implementqmp_guest_get_memory_blocks()for Linux with sysfs
qga: implementqmp_guest_set_memory_blocks()for Linux with sysfs
qga: implementqmp_guest_get_memory_block_size()for Linux with sysfs
qga: add memory block command that unsupported to blacklistqga/commands-posix.c | 324 ++++++++++++++++++++++++++++++++++++++++++++++++++- qga/commands-win32.c | 21 ++++ qga/qapi-schema.json | 114 ++++++++++++++++++ 3 files changed, 458 insertions(+), 1 deletion(-)
Michael Roth
AppliedQuoting zhanghailiang (2015-01-21 20:40:01)
Quoted 7 lines
This patch series add three guest commands about memory block:
guest-get-memory-blocks, guest-set-memory-blocks, guest-get-memory-block-size.With these three commands, we can get information about guest's memory block online/offline status and memory block size (unit of memory online/offline operation ). Also, we can change guest's memory block status (Logical memory hotplug/unplug) from host.
Thanks, applied to QGA tree:
https://github.com/mdroth/qemu/commits/qga
with a small change to patch 2 that replaces an assertion that the block list isn't NULL with a reported error instead. This is more of an assertion about the guest OS than the code itself, so it's best to avoid tying the life-cycle of the agent to it. Otherwise looks good.
Quoted 33 lines
Example of usage:
{"execute":"guest-get-memory-blocks"} {"return":[{"online":true,"can-offline":false,"phys-index":0},{"online":true,"can-offline":true,"phys-index":1},{"online":true,"can-offline":false,"phys-index":2},{"online":true,"can-offline":false,"phys-index":3}]}
{"execute":"guest-set-memory-blocks","arguments":{"mem-blks":[{"phys-index":0,"online":false},{"phys-index":1,"online":false},{"phys-index":3,"online":false}]}} {"return":[{"response":"operation-failed","error-code":22,"phys-index":0},{"response":"success","phys-index":1},{"response":"operation-failed","error-code":16,"phys-index":3}]}
{"execute":"guest-get-memory-block-size"}
{"return":134217728}v2:
- Change return value of 'guest-set-memory-blocks' command from 'int' to a list of 'GuestMemoryBlockResponse', which contains more info about the operation results. It is suggested by Michael Roth. It is more useful for callers to know the exact operation result for each memory block, and also they can help callers to decide what to do next according to different error-code.zhanghailiang (5):
qga: introduce three guest memory block commmands with stubs
qga: implementqmp_guest_get_memory_blocks()for Linux with sysfs
qga: implementqmp_guest_set_memory_blocks()for Linux with sysfs
qga: implementqmp_guest_get_memory_block_size()for Linux with sysfs
qga: add memory block command that unsupported to blacklistqga/commands-posix.c | 324 ++++++++++++++++++++++++++++++++++++++++++++++++++- qga/commands-win32.c | 21 ++++ qga/qapi-schema.json | 114 ++++++++++++++++++ 3 files changed, 458 insertions(+), 1 deletion(-)Signature · 1 line
1.7.12.4