Re: [libvirt] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
On 08/09/2012 08:10 PM, Corey Bryant wrote: This patch adds support that enables passing of file descriptors to the QEMU monitor where they will be stored in specified file descriptor sets. A file descriptor set can be used by a client like libvirt to store file descriptors for the same file. This allows the client to open a file with different access modes (O_RDWR, O_WRONLY, O_RDONLY) and add/remove the passed fds to/from an fd set as needed. This will allow QEMU to (in a later patch in this series) open and reopen the same file by dup()ing the fd in the fd set that corresponds to the file, where the fd has the matching access mode flag that QEMU requests. The new QMP commands are: add-fd: Add a file descriptor to an fd set remove-fd: Remove a file descriptor from an fd set query-fdsets: Return information describing all fd sets Note: These commands are not compatible with the existing getfd and closefd QMP commands. Signed-off-by: Corey Bryant cor...@linux.vnet.ibm.com +void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +char fd_str[20]; + +QLIST_FOREACH(mon_fdset, mon_fdsets, next) { +if (mon_fdset-id != fdset_id) { +continue; +} +QLIST_FOREACH(mon_fdset_fd, mon_fdset-fds, next) { +if (has_fd mon_fdset_fd-fd != fd) { +continue; +} +mon_fdset_fd-removed = true; +if (has_fd) { +break; +} +} +monitor_fdset_cleanup(mon_fdset); +return; +} +snprintf(fd_str, sizeof(fd_str), % PRId64, fd); +error_set(errp, QERR_FD_NOT_FOUND, fd_str); This error catches the case of fdset_id not found, but you never raise an error when has_fd is true but fd is not found within fdset_id. You also don't raise an error for back-to-back remove-fd(fdset-id=1,fd=3), because you aren't checking whether mon_fdset_fd-removed was already true. I'm not sure which semantic is better, but I _am_ worried that we have both semantics in teh same function (are we arguing that this command succeeds when the fd is gone, even if it already was gone? Or are we arguing that this command diagnoses failure on an attempt to remove something that doesn't exist). I guess I'm 60-40 towards always issuing an error on an attempt to remove something that can't be found or is already removed. +} + +FdsetInfoList *qmp_query_fdsets(Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +FdsetInfoList *fdset_list = NULL; + +QLIST_FOREACH(mon_fdset, mon_fdsets, next) { +FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info)); +FdsetFdInfoList *fdsetfd_list = NULL; + +fdset_info-value = g_malloc0(sizeof(*fdset_info-value)); +fdset_info-value-fdset_id = mon_fdset-id; + +QLIST_FOREACH(mon_fdset_fd, mon_fdset-fds, next) { +FdsetFdInfoList *fdsetfd_info; + +if (mon_fdset_fd-removed) { +continue; +} This means that an fdset with all fds removed will show up as empty in the output; I guess that's okay, as libvirt could use that to infer that the dup()d fds are still in use. The alternative is to keep track of whether we have output any information about an fd within a set before including the set itself in the overall output. +++ b/qapi-schema.json +## +# @add-fd: +# +# Add a file descriptor, that was passed via SCM rights, to an fd set. +# +# @fdset-id: #optional The ID of the fd set to add the file descriptor to. +# +# @opaque: #optional A free-form string that can be used to describe the fd. +# +# Returns: @AddfdInfo on success +# If file descriptor was not received, FdNotSupplied +# If @fdset_id does not exist, InvalidParameterValue Missed one: s/_/-/ +## +# @remove-fd: +# +# Remove a file descriptor from an fd set. +# +# @fdset-id: The ID of the fd set that the file descriptor belongs to. +# +# @fd: #optional The file descriptor that is to be removed. +# +# Returns: Nothing on success +# If @fdset_id or @fd is not found, FdNotFound and another s/_/-/ +SQMP +query-fdsets +- + +Return information describing all fd sets. + +Arguments: None + +Example: + +- { execute: query-fdsets } +- { return: [ + { + fdset-id: 1 missing a comma + fds: [ + { + fd: 21, JSON does not permit trailing commas. + }, + { + fd: 23, and again + } + ], + }, + { + fdset-id: 2 missing a comma + fds: [ + { + fd: 22, trailing comma Also, it might be nice to include something like: opaque:rdonly:/path/to/file in one of the examples to give a hint to the reader how to use opaque. -- Eric
Re: [libvirt] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
On Thu, Aug 09, 2012 at 10:10:44PM -0400, Corey Bryant wrote: +void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +char fd_str[20]; + +QLIST_FOREACH(mon_fdset, mon_fdsets, next) { +if (mon_fdset-id != fdset_id) { +continue; +} +QLIST_FOREACH(mon_fdset_fd, mon_fdset-fds, next) { +if (has_fd mon_fdset_fd-fd != fd) { +continue; +} +mon_fdset_fd-removed = true; +if (has_fd) { +break; +} +} +monitor_fdset_cleanup(mon_fdset); +return; +} +snprintf(fd_str, sizeof(fd_str), % PRId64, fd); +error_set(errp, QERR_FD_NOT_FOUND, fd_str); fd is optional and may be uninitialized. I think the human-readable string should be: if has_fd: fd_str = '%s:%s' % (fdset_id, fd) else: fd_str = '%s' % fdset_id Otherwise, looks good. -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
On 08/10/2012 03:20 AM, Stefan Hajnoczi wrote: On Thu, Aug 09, 2012 at 10:10:44PM -0400, Corey Bryant wrote: +void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp) +{ +MonFdset *mon_fdset; +MonFdsetFd *mon_fdset_fd; +char fd_str[20]; + +QLIST_FOREACH(mon_fdset, mon_fdsets, next) { +if (mon_fdset-id != fdset_id) { +continue; +} +QLIST_FOREACH(mon_fdset_fd, mon_fdset-fds, next) { +if (has_fd mon_fdset_fd-fd != fd) { +continue; +} +mon_fdset_fd-removed = true; +if (has_fd) { +break; +} +} +monitor_fdset_cleanup(mon_fdset); +return; +} +snprintf(fd_str, sizeof(fd_str), % PRId64, fd); +error_set(errp, QERR_FD_NOT_FOUND, fd_str); fd is optional and may be uninitialized. I think the human-readable string should be: if has_fd: fd_str = '%s:%s' % (fdset_id, fd) else: fd_str = '%s' % fdset_id Otherwise, looks good. Good point, thanks. I'll fix this. -- Regards, Corey -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
Am 10.08.2012 04:10, schrieb Corey Bryant: This patch adds support that enables passing of file descriptors to the QEMU monitor where they will be stored in specified file descriptor sets. A file descriptor set can be used by a client like libvirt to store file descriptors for the same file. This allows the client to open a file with different access modes (O_RDWR, O_WRONLY, O_RDONLY) and add/remove the passed fds to/from an fd set as needed. This will allow QEMU to (in a later patch in this series) open and reopen the same file by dup()ing the fd in the fd set that corresponds to the file, where the fd has the matching access mode flag that QEMU requests. The new QMP commands are: add-fd: Add a file descriptor to an fd set remove-fd: Remove a file descriptor from an fd set query-fdsets: Return information describing all fd sets Note: These commands are not compatible with the existing getfd and closefd QMP commands. Signed-off-by: Corey Bryant cor...@linux.vnet.ibm.com --- v5: -This patch is new in v5 and replaces the pass-fd QMP command from v4. -By grouping fds in fd sets, we ease managability with an fd set per file, addressing concerns raised in v4 about handling reopens and preventing fd leakage. (ebl...@redhat.com, kw...@redhat.com, dberra...@redhat.com) v6 -Make @fd optional for remove-fd (ebl...@redhat.com) -Make @fdset-id optional for add-fd (ebl...@redhat.com) v7: -Share fd sets among all monitor connections (kw...@redhat.com) -Added mon_refcount to keep track of monitor connection count. v8: -Add opaque string to add-fd/query-fdsets. (stefa...@linux.vnet.ibm.com) -Use camel case for structures. (stefa...@linux.vnet.ibm.com) -Don't return in-use and refcount from query-fdsets. (stefa...@linux.vnet.ibm.com) -Don't return removed fd's from query-fdsets. (stefa...@linux.vnet.ibm.com) -Use fdset-id rather than fdset_id. (ebl...@redhat.com) -Fix fd leak in qmp_add_fd(). (stefa...@linux.vnet.ibm.com) -Update QMP errors. (stefa...@linux.vnet.ibm.com, ebl...@redhat.com) monitor.c| 188 ++ qapi-schema.json | 98 qmp-commands.hx | 117 + 3 files changed, 403 insertions(+) diff --git a/monitor.c b/monitor.c index 49dccfe..f9a0577 100644 --- a/monitor.c +++ b/monitor.c @@ -140,6 +140,24 @@ struct mon_fd_t { QLIST_ENTRY(mon_fd_t) next; }; +/* file descriptor associated with a file descriptor set */ +typedef struct MonFdsetFd MonFdsetFd; +struct MonFdsetFd { +int fd; +bool removed; +char *opaque; +QLIST_ENTRY(MonFdsetFd) next; +}; + +/* file descriptor set containing fds passed via SCM_RIGHTS */ +typedef struct MonFdset MonFdset; +struct MonFdset { +int64_t id; +int refcount; +QLIST_HEAD(, MonFdsetFd) fds; +QLIST_ENTRY(MonFdset) next; +}; + typedef struct MonitorControl { QObject *id; JSONMessageParser parser; @@ -211,6 +229,8 @@ static inline int mon_print_count_get(const Monitor *mon) { return 0; } #define QMP_ACCEPT_UNKNOWNS 1 static QLIST_HEAD(mon_list, Monitor) mon_list; +static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; +static int mon_refcount; You introduce mon_refcount in this patch and check it against 0 in one place, but it never gets changed until patch 3 is applied. Would it make sense to do both in the same patch? Kevin -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v8 2/7] qapi: Introduce add-fd, remove-fd, query-fdsets
On 08/10/2012 12:08 PM, Kevin Wolf wrote: Am 10.08.2012 04:10, schrieb Corey Bryant: This patch adds support that enables passing of file descriptors to the QEMU monitor where they will be stored in specified file descriptor sets. A file descriptor set can be used by a client like libvirt to store file descriptors for the same file. This allows the client to open a file with different access modes (O_RDWR, O_WRONLY, O_RDONLY) and add/remove the passed fds to/from an fd set as needed. This will allow QEMU to (in a later patch in this series) open and reopen the same file by dup()ing the fd in the fd set that corresponds to the file, where the fd has the matching access mode flag that QEMU requests. The new QMP commands are: add-fd: Add a file descriptor to an fd set remove-fd: Remove a file descriptor from an fd set query-fdsets: Return information describing all fd sets Note: These commands are not compatible with the existing getfd and closefd QMP commands. Signed-off-by: Corey Bryant cor...@linux.vnet.ibm.com --- v5: -This patch is new in v5 and replaces the pass-fd QMP command from v4. -By grouping fds in fd sets, we ease managability with an fd set per file, addressing concerns raised in v4 about handling reopens and preventing fd leakage. (ebl...@redhat.com, kw...@redhat.com, dberra...@redhat.com) v6 -Make @fd optional for remove-fd (ebl...@redhat.com) -Make @fdset-id optional for add-fd (ebl...@redhat.com) v7: -Share fd sets among all monitor connections (kw...@redhat.com) -Added mon_refcount to keep track of monitor connection count. v8: -Add opaque string to add-fd/query-fdsets. (stefa...@linux.vnet.ibm.com) -Use camel case for structures. (stefa...@linux.vnet.ibm.com) -Don't return in-use and refcount from query-fdsets. (stefa...@linux.vnet.ibm.com) -Don't return removed fd's from query-fdsets. (stefa...@linux.vnet.ibm.com) -Use fdset-id rather than fdset_id. (ebl...@redhat.com) -Fix fd leak in qmp_add_fd(). (stefa...@linux.vnet.ibm.com) -Update QMP errors. (stefa...@linux.vnet.ibm.com, ebl...@redhat.com) monitor.c| 188 ++ qapi-schema.json | 98 qmp-commands.hx | 117 + 3 files changed, 403 insertions(+) diff --git a/monitor.c b/monitor.c index 49dccfe..f9a0577 100644 --- a/monitor.c +++ b/monitor.c @@ -140,6 +140,24 @@ struct mon_fd_t { QLIST_ENTRY(mon_fd_t) next; }; +/* file descriptor associated with a file descriptor set */ +typedef struct MonFdsetFd MonFdsetFd; +struct MonFdsetFd { +int fd; +bool removed; +char *opaque; +QLIST_ENTRY(MonFdsetFd) next; +}; + +/* file descriptor set containing fds passed via SCM_RIGHTS */ +typedef struct MonFdset MonFdset; +struct MonFdset { +int64_t id; +int refcount; +QLIST_HEAD(, MonFdsetFd) fds; +QLIST_ENTRY(MonFdset) next; +}; + typedef struct MonitorControl { QObject *id; JSONMessageParser parser; @@ -211,6 +229,8 @@ static inline int mon_print_count_get(const Monitor *mon) { return 0; } #define QMP_ACCEPT_UNKNOWNS 1 static QLIST_HEAD(mon_list, Monitor) mon_list; +static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; +static int mon_refcount; You introduce mon_refcount in this patch and check it against 0 in one place, but it never gets changed until patch 3 is applied. Would it make sense to do both in the same patch? Yes, I'll go ahead and move the mon_refcount code from this patch to patch 3. -- Regards, Corey -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list