diff mbox series

[V2,18/22] chardev: cpr for sockets

Message ID 1609861330-129855-19-git-send-email-steven.sistare@oracle.com (mailing list archive)
State New, archived
Headers show
Series Live Update | expand

Commit Message

Steven Sistare Jan. 5, 2021, 3:42 p.m. UTC
Define qio_channel_socket_reuse to initialize a channel based on an existing
socket fd.  Save accepted socket fds in the environment before cprsave, and
look for fds in the environment after cprload.  Reject cprsave if a socket
enables the TLS or websocket option.

Signed-off-by: Mark Kanda <mark.kanda@oracle.com>
Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
 chardev/char-socket.c       | 30 ++++++++++++++++++++++++++++++
 include/io/channel-socket.h | 12 ++++++++++++
 io/channel-socket.c         |  9 +++++++++
 stubs/Makefile.objs         |  1 +
 stubs/cpr.c                 |  3 +++
 5 files changed, 55 insertions(+)
 create mode 100644 stubs/cpr.c

Comments

Daniel P. Berrangé Jan. 5, 2021, 4:22 p.m. UTC | #1
On Tue, Jan 05, 2021 at 07:42:06AM -0800, Steve Sistare wrote:
> Define qio_channel_socket_reuse to initialize a channel based on an existing
> socket fd.  Save accepted socket fds in the environment before cprsave, and
> look for fds in the environment after cprload.  Reject cprsave if a socket
> enables the TLS or websocket option.
> 
> Signed-off-by: Mark Kanda <mark.kanda@oracle.com>
> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
> ---
>  chardev/char-socket.c       | 30 ++++++++++++++++++++++++++++++
>  include/io/channel-socket.h | 12 ++++++++++++
>  io/channel-socket.c         |  9 +++++++++
>  stubs/Makefile.objs         |  1 +
>  stubs/cpr.c                 |  3 +++
>  5 files changed, 55 insertions(+)
>  create mode 100644 stubs/cpr.c
> 

> diff --git a/include/io/channel-socket.h b/include/io/channel-socket.h
> index 777ff59..e425a01 100644
> --- a/include/io/channel-socket.h
> +++ b/include/io/channel-socket.h
> @@ -260,5 +260,17 @@ QIOChannelSocket *
>  qio_channel_socket_accept(QIOChannelSocket *ioc,
>                            Error **errp);
>  
> +/**
> + * qio_channel_socket_reuse:
> + * @fd: existing client socket descriptor
> + * @errp: pointer to a NULL-initialized error object
> + *
> + * Construct a client channel using @fd.
> + *
> + * Returns: the new client channel, or NULL on error
> + */
> +QIOChannelSocket *
> +qio_channel_socket_reuse(int fd,
> +                         Error **errp);
>  
>  #endif /* QIO_CHANNEL_SOCKET_H */
> diff --git a/io/channel-socket.c b/io/channel-socket.c
> index de49880..07981be 100644
> --- a/io/channel-socket.c
> +++ b/io/channel-socket.c
> @@ -400,6 +400,15 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,
>      return NULL;
>  }
>  
> +QIOChannelSocket *
> +qio_channel_socket_reuse(int fd,
> +                         Error **errp)
> +{
> +    QIOChannelSocket *cioc = qio_channel_socket_new();
> +    cioc->fd = fd;
> +    return qio_channel_socket_post_accept(cioc, errp) ? 0 : cioc;
> +}

Why do we need to add this new API when we already have

 qio_channel_socket_new_fd(int fd, Error **errp)

which accepts a pre-opened socket FD ?


Regards,
Daniel
Steven Sistare Jan. 5, 2021, 4:35 p.m. UTC | #2
On 1/5/2021 11:22 AM, Daniel P. Berrangé wrote:
> On Tue, Jan 05, 2021 at 07:42:06AM -0800, Steve Sistare wrote:
>> Define qio_channel_socket_reuse to initialize a channel based on an existing
>> socket fd.  Save accepted socket fds in the environment before cprsave, and
>> look for fds in the environment after cprload.  Reject cprsave if a socket
>> enables the TLS or websocket option.
>>
>> Signed-off-by: Mark Kanda <mark.kanda@oracle.com>
>> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
>> ---
>>  chardev/char-socket.c       | 30 ++++++++++++++++++++++++++++++
>>  include/io/channel-socket.h | 12 ++++++++++++
>>  io/channel-socket.c         |  9 +++++++++
>>  stubs/Makefile.objs         |  1 +
>>  stubs/cpr.c                 |  3 +++
>>  5 files changed, 55 insertions(+)
>>  create mode 100644 stubs/cpr.c
>>
> 
>> diff --git a/include/io/channel-socket.h b/include/io/channel-socket.h
>> index 777ff59..e425a01 100644
>> --- a/include/io/channel-socket.h
>> +++ b/include/io/channel-socket.h
>> @@ -260,5 +260,17 @@ QIOChannelSocket *
>>  qio_channel_socket_accept(QIOChannelSocket *ioc,
>>                            Error **errp);
>>  
>> +/**
>> + * qio_channel_socket_reuse:
>> + * @fd: existing client socket descriptor
>> + * @errp: pointer to a NULL-initialized error object
>> + *
>> + * Construct a client channel using @fd.
>> + *
>> + * Returns: the new client channel, or NULL on error
>> + */
>> +QIOChannelSocket *
>> +qio_channel_socket_reuse(int fd,
>> +                         Error **errp);
>>  
>>  #endif /* QIO_CHANNEL_SOCKET_H */
>> diff --git a/io/channel-socket.c b/io/channel-socket.c
>> index de49880..07981be 100644
>> --- a/io/channel-socket.c
>> +++ b/io/channel-socket.c
>> @@ -400,6 +400,15 @@ qio_channel_socket_accept(QIOChannelSocket *ioc,
>>      return NULL;
>>  }
>>  
>> +QIOChannelSocket *
>> +qio_channel_socket_reuse(int fd,
>> +                         Error **errp)
>> +{
>> +    QIOChannelSocket *cioc = qio_channel_socket_new();
>> +    cioc->fd = fd;
>> +    return qio_channel_socket_post_accept(cioc, errp) ? 0 : cioc;
>> +}
> 
> Why do we need to add this new API when we already have
> 
>  qio_channel_socket_new_fd(int fd, Error **errp)
> 
> which accepts a pre-opened socket FD ?

That was fast!
Good call, thanks.  I missed that qio_channel_socket_new_fd calls qio_channel_socket_set_fd and
the latter performs the necessary post-accept actions.  I will also delete patch 17.

- Steve
diff mbox series

Patch

diff --git a/chardev/char-socket.c b/chardev/char-socket.c
index ef62dbf..0965305 100644
--- a/chardev/char-socket.c
+++ b/chardev/char-socket.c
@@ -36,6 +36,7 @@ 
 #include "qapi/qapi-visit-sockets.h"
 
 #include "chardev/char-io.h"
+#include "qemu/env.h"
 
 /***********************************************************/
 /* TCP Net console */
@@ -400,6 +401,7 @@  static void tcp_chr_free_connection(Chardev *chr)
     SocketChardev *s = SOCKET_CHARDEV(chr);
     int i;
 
+    unsetenv_fd(chr->label);
     if (s->read_msgfds_num) {
         for (i = 0; i < s->read_msgfds_num; i++) {
             close(s->read_msgfds[i]);
@@ -1157,6 +1159,25 @@  static gboolean socket_reconnect_timeout(gpointer opaque)
     return false;
 }
 
+static void load_char_socket_fd(Chardev *chr)
+{
+    SocketChardev *sockchar = SOCKET_CHARDEV(chr);
+    QIOChannelSocket *sioc;
+    int fd = getenv_fd(chr->label);
+
+    if (fd != -1) {
+        sockchar = SOCKET_CHARDEV(chr);
+        sioc = qio_channel_socket_reuse(fd, NULL);
+        if (sioc) {
+            tcp_chr_accept(sockchar->listener, sioc, chr);
+        } else {
+            error_printf("error: could not restore socket for %s\n",
+                         chr->label);
+        }
+    } else if (sockchar->sioc && !chr->close_on_cpr) {
+        setenv_fd(chr->label, sockchar->sioc->fd);
+    }
+}
 
 static int qmp_chardev_open_socket_server(Chardev *chr,
                                           bool is_telnet,
@@ -1360,6 +1381,13 @@  static void qmp_chardev_open_socket(Chardev *chr,
         qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
     }
 
+    if (!s->tls_creds && !s->is_websock) {
+        qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_CPR);
+    } else if (only_cpr_capable) {
+        error_setg(errp, "error: socket %s is not cpr capable due to %s option",
+                   chr->label, (s->tls_creds ? "TLS" : "websocket"));
+    }
+
     /* be isn't opened until we get a connection */
     *be_opened = false;
 
@@ -1375,6 +1403,8 @@  static void qmp_chardev_open_socket(Chardev *chr,
             return;
         }
     }
+
+    load_char_socket_fd(chr);
 }
 
 static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
diff --git a/include/io/channel-socket.h b/include/io/channel-socket.h
index 777ff59..e425a01 100644
--- a/include/io/channel-socket.h
+++ b/include/io/channel-socket.h
@@ -260,5 +260,17 @@  QIOChannelSocket *
 qio_channel_socket_accept(QIOChannelSocket *ioc,
                           Error **errp);
 
+/**
+ * qio_channel_socket_reuse:
+ * @fd: existing client socket descriptor
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Construct a client channel using @fd.
+ *
+ * Returns: the new client channel, or NULL on error
+ */
+QIOChannelSocket *
+qio_channel_socket_reuse(int fd,
+                         Error **errp);
 
 #endif /* QIO_CHANNEL_SOCKET_H */
diff --git a/io/channel-socket.c b/io/channel-socket.c
index de49880..07981be 100644
--- a/io/channel-socket.c
+++ b/io/channel-socket.c
@@ -400,6 +400,15 @@  qio_channel_socket_accept(QIOChannelSocket *ioc,
     return NULL;
 }
 
+QIOChannelSocket *
+qio_channel_socket_reuse(int fd,
+                         Error **errp)
+{
+    QIOChannelSocket *cioc = qio_channel_socket_new();
+    cioc->fd = fd;
+    return qio_channel_socket_post_accept(cioc, errp) ? 0 : cioc;
+}
+
 static void qio_channel_socket_init(Object *obj)
 {
     QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(obj);
diff --git a/stubs/Makefile.objs b/stubs/Makefile.objs
index d42046a..f6c335b 100644
--- a/stubs/Makefile.objs
+++ b/stubs/Makefile.objs
@@ -1,5 +1,6 @@ 
 stub-obj-y += blk-commit-all.o
 stub-obj-y += cmos.o
+stub-obj-y += cpr.o
 stub-obj-y += cpu-get-clock.o
 stub-obj-y += cpu-get-icount.o
 stub-obj-y += dump.o
diff --git a/stubs/cpr.c b/stubs/cpr.c
new file mode 100644
index 0000000..aaa189e
--- /dev/null
+++ b/stubs/cpr.c
@@ -0,0 +1,3 @@ 
+#include "qemu/osdep.h"
+
+bool only_cpr_capable;