Skip to contentSkip to conversation

vchan-socket-proxy: add reconnect marker support

PATCH In review Marek Marczykowski-Górecki 3 participants 8 messages Sep 5, 2022 – Dec 7, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 vchan-socket-proxy: add reconnect marker support

When vchan client reconnect quickly, the server may not notice it. This means, it won't reconnect the UNIX socket either. For QMP, it will prevent the client to see the QMP protocol handshake, and the communication will timeout.
Solve the issue by sending in-band connect marker. Whenever server sees one (elsewhere than the first byte in the connection), handle it as a client had reconnected. The marker is a one byte, and the user need to choose something that doesn't appear in the data stream elsewhere.

---
 tools/vchan/vchan-socket-proxy.c | 51 +++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
tools/vchan/vchan-socket-proxy.c +50 -1
index e1d959c6d15c..1e7defe9bae7 100644
@@ -31,6 +31,7 @@
  * One client is served at a time, clients needs to coordinate this themselves.
  */
 
+#define _GNU_SOURCE
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -54,6 +55,9 @@ static void usage(char** argv)
         "\t-m, --mode=client|server - vchan connection mode (client by default)\n"
         "\t-s, --state-path=path - xenstore path where write \"running\" to \n"
         "\t                        at startup\n"
+        "\t-r, --reconnect-marker=value - send(client)/expect(server) a\n"
+        "\t                single-byte marker to detect quick reconnects and\n"
+        "\t                force reconnecting UNIX socket\n"
         "\t-v, --verbose - verbose logging\n"
         "\n"
         "client: client of a vchan connection, fourth parameter can be:\n"
@@ -61,7 +65,7 @@ static void usage(char** argv)
         "\t             whenever new connection is accepted;\n"
         "\t             handle multiple _subsequent_ connections, until terminated\n"
         "\n"
-        "\tfile-no:     except open FD of a socket in listen mode;\n"
+        "\tfile-no:     expect open FD of a socket in listen mode;\n"
         "\t             otherwise similar to socket-path\n"
         "\n"
         "\t-:           open vchan connection immediately and pass the data\n"
@@ -88,6 +92,7 @@ char outbuf[BUFSIZE];
 int insiz = 0;
 int outsiz = 0;
 int verbose = 0;
+int reconnect_marker_value = -1;
 
 struct vchan_proxy_state {
     struct libxenvchan *ctrl;
@@ -291,6 +296,7 @@ int data_loop(struct vchan_proxy_state *state)
     int ret;
     int libxenvchan_fd;
     int max_fd;
+    bool just_connected = true;
 
     libxenvchan_fd = libxenvchan_fd_for_select(state->ctrl);
     for (;;) {
@@ -368,8 +374,33 @@ int data_loop(struct vchan_proxy_state *state)
                 exit(1);
             if (verbose)
                 fprintf(stderr, "from-vchan: %.*s\n", ret, outbuf + outsiz);
+            if (reconnect_marker_value != -1) {
+                char *reconnect_found =
+                    memrchr(outbuf + outsiz, reconnect_marker_value, ret);
+                if (just_connected && reconnect_found == outbuf + outsiz) {
+                    /* skip reconnect marker at the very first byte of the data
+                     * stream */
+                    memmove(outbuf + outsiz, outbuf + outsiz + 1, ret - 1);
+                    ret -= 1;
+                } else if (reconnect_found) {
+                    size_t newsiz = outbuf + outsiz + ret - reconnect_found - 1;
+                    if (verbose)
+                        fprintf(stderr, "reconnect marker found\n");
+                    /* discard everything before and including the reconnect
+                     * marker */
+                    memmove(outbuf, reconnect_found + 1, newsiz);
+                    outsiz = newsiz;
+                    /* then handle it as the client had just disconnected */
+                    close(state->output_fd);
+                    state->output_fd = -1;
+                    close(state->input_fd);
+                    state->input_fd = -1;
+                    return 0;
+                }
+            }
             outsiz += ret;
             socket_wr(state->output_fd);
+            just_connected = false;
         }
     }
     return 0;
@@ -385,6 +416,7 @@ static struct option options[] = {
     { "mode",       required_argument, NULL, 'm' },
     { "verbose",          no_argument, NULL, 'v' },
     { "state-path", required_argument, NULL, 's' },
+    { "reconnect-marker", required_argument, NULL, 'r' },
     { }
 };
 
@@ -421,6 +453,14 @@ int main(int argc, char **argv)
             case 's':
                 state_path = optarg;
                 break;
+            case 'r':
+                reconnect_marker_value = atoi(optarg);
+                if (reconnect_marker_value < 0 || reconnect_marker_value > 255) {
+                    fprintf(stderr, "invalid argument for --reconnect-marker, "
+                                    "must be a number between 0 and 255\n");
+                    usage(argv);
+                }
+                break;
             case '?':
                 usage(argv);
         }
@@ -509,6 +549,15 @@ int main(int argc, char **argv)
                 ret = 1;
                 break;
             }
+            if (reconnect_marker_value != -1) {
+                const char marker_buf[] = { reconnect_marker_value };
+
+                if (libxenvchan_write(state.ctrl, marker_buf, sizeof(marker_buf))
+                        != sizeof(marker_buf)) {
+                    fprintf(stderr, "failed to send reconnect marker\n");
+                    break;
+                }
+            }
             if (data_loop(&state) != 0)
                 break;
             /* don't reconnect if output was stdout */
Signature · 1 line
2.35.3
#1 lore raw

PATCH 2/2 tools/libxl: enable in-band reconnect marker for stubdom QMP proxy

This enables stubdom reliably detect when it needs to reconnect QMP socket. It is critical, as otherwise QEMU will not send its handshake, and so libxl will timeout while waiting on one. When it happens during domain startup, it can result in error like this:

libxl: libxl_pci.c:1772:device_pci_add_done: Domain 3:libxl__device_pci_add failed for PCI device 0:0:14.0 (rc -9)
libxl: libxl_create.c:1904:domcreate_attach_devices: Domain 3:unable to add pci devices

See vchan-socket-proxy commit message for details about this reconnect corner case.

Stubdomain side needs to use --reconnect-marker=1 option too.

---
 tools/libs/light/libxl_dm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/libs/light/libxl_dm.c +2 -1
index fc264a3a13a6..cc9c5bea1e7f 100644
@@ -2625,10 +2625,11 @@ static void spawn_qmp_proxy(libxl__egc *egc,
     sdss->qmp_proxy_spawn.failure_cb = qmp_proxy_startup_failed;
     sdss->qmp_proxy_spawn.detached_cb = qmp_proxy_detached;
 
-    const int arraysize = 6;
+    const int arraysize = 7;
     GCNEW_ARRAY(args, arraysize);
     args[nr++] = STUBDOM_QMP_PROXY_PATH;
     args[nr++] = GCSPRINTF("--state-path=%s", sdss->qmp_proxy_spawn.xspath);
+    args[nr++] = "--reconnect-marker=1";
     args[nr++] = GCSPRINTF("%u", dm_domid);
     args[nr++] = GCSPRINTF("%s/device-model/%u/qmp-vchan", dom_path, guest_domid);
     args[nr++] = (char*)libxl__qemu_qmp_path(gc, guest_domid);
Signature · 1 line
2.35.3
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] tools/libxl: enable in-band reconnect marker for stubdom QMP proxy

Quoted 9 lines · Marek Marczykowski-Górecki

This enables stubdom reliably detect when it needs to reconnect QMP socket. It is critical, as otherwise QEMU will not send its handshake, and so libxl will timeout while waiting on one. When it happens during domain startup, it can result in error like this:

libxl: libxl_pci.c:1772:device_pci_add_done: Domain 3:libxl__device_pci_add failed for PCI device 0:0:14.0 (rc -9)
libxl: libxl_create.c:1904:domcreate_attach_devices: Domain 3:unable to add pci devices

Quoted 6 lines

See vchan-socket-proxy commit message for details about this reconnect corner case.

Stubdomain side needs to use --reconnect-marker=1 option too.

Tested-by
#3 lore raw

Re: [PATCH 2/2] tools/libxl: enable in-band reconnect marker for stubdom QMP proxy

Quoted 12 lines · Jason Andryuk
Quoted 9 lines · Marek Marczykowski-Górecki

This enables stubdom reliably detect when it needs to reconnect QMP socket. It is critical, as otherwise QEMU will not send its handshake, and so libxl will timeout while waiting on one. When it happens during domain startup, it can result in error like this:

libxl: libxl_pci.c:1772:device_pci_add_done: Domain 3:libxl__device_pci_add failed for PCI device 0:0:14.0 (rc -9)
libxl: libxl_create.c:1904:domcreate_attach_devices: Domain 3:unable to add pci devices

Quoted 8 lines
Quoted 6 lines

See vchan-socket-proxy commit message for details about this reconnect corner case.

Stubdomain side needs to use --reconnect-marker=1 option too.

Also

Acked-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] tools/libxl: enable in-band reconnect marker for stubdom QMP proxy

In reply to Marek Marczykowski-Górecki
Quoted 8 lines · Marek Marczykowski-Górecki

This enables stubdom reliably detect when it needs to reconnect QMP socket. It is critical, as otherwise QEMU will not send its handshake, and so libxl will timeout while waiting on one. When it happens during domain startup, it can result in error like this:

libxl: libxl_pci.c:1772:device_pci_add_done: Domain 3:libxl__device_pci_add failed for PCI device 0:0:14.0 (rc -9)
libxl: libxl_create.c:1904:domcreate_attach_devices: Domain 3:unable to add pci devices

Quoted 6 lines

See vchan-socket-proxy commit message for details about this reconnect corner case.

Stubdomain side needs to use --reconnect-marker=1 option too.

Thanks,

Signature · 1 line
Anthony PERARD
Reviewed-by on v1 1/2
#5 lore raw
In reply to Marek Marczykowski-Górecki
Quoted 11 lines · Marek Marczykowski-Górecki

When vchan client reconnect quickly, the server may not notice it. This means, it won't reconnect the UNIX socket either. For QMP, it will prevent the client to see the QMP protocol handshake, and the communication will timeout.
Solve the issue by sending in-band connect marker. Whenever server sees one (elsewhere than the first byte in the connection), handle it as a client had reconnected. The marker is a one byte, and the user need to choose something that doesn't appear in the data stream elsewhere.

Here are some of my thoughts on the correctness of this approach:
The QMP data is json - ascii (utf-8?) - so using \x01 is fine as it won't be in the data stream. The dropping of any partial message works since the close and re-open of the QMP socket resets the data stream. There shouldn't be any partial data in vchan-socket-proxy to drop since the client side should not have sent any new data before it closed and re-opened. If there is partial data, we shouldn't send it into QEMU since the new QMP client isn't expecting any response it would generate. So that all seems okay.

Regards,
Jason

Acked-by on v1 1/2
#6 lore raw
In reply to Marek Marczykowski-Górecki
Quoted 1 line · Marek Marczykowski-Górecki

+ reconnect_marker_value = atoi(optarg);

atoi() isn't great, if there's garbage it just return 0, which is validated below.

Would there be value here in using strtol() which can detect input error? To at least help with maybe hard to debug issues?

Quoted 10 lines
tools/vchan/vchan-socket-proxy.c +6 -0
+                if (reconnect_marker_value < 0 || reconnect_marker_value > 255) {
+                    fprintf(stderr, "invalid argument for --reconnect-marker, "
+                                    "must be a number between 0 and 255\n");
+                    usage(argv);
+                }
+                break;
             case '?':
                 usage(argv);
         }

tools/vchan/vchan-socket-proxy.c int main(int argc, char **argv) · line 557

Quoted 9 lines
tools/vchan/vchan-socket-proxy.c +6 -0
@@ -509,6 +549,15 @@ int main(int argc, char **argv)
                 ret = 1;
                 break;
             }
+            if (reconnect_marker_value != -1) {
+                const char marker_buf[] = { reconnect_marker_value };
+
+                if (libxenvchan_write(state.ctrl, marker_buf, sizeof(marker_buf))
+                        != sizeof(marker_buf)) {
+                    fprintf(stderr, "failed to send reconnect marker\n");

Is this an expected failure? If so, maybe adding "(ignored)" might be valuable to someone reading the logs?

tools/vchan/vchan-socket-proxy.c int main(int argc, char **argv) · line 563

Quoted 6 lines
tools/vchan/vchan-socket-proxy.c +3 -0
+                    break;
+                }
+            }
             if (data_loop(&state) != 0)
                 break;
             /* don't reconnect if output was stdout */

Otherwise, the patch looks fine. Even if kept as is:

Thanks,

Signature · 1 line
Anthony PERARD
#7 lore raw
Quoted 8 lines · Anthony PERARD
Quoted 1 line · Marek Marczykowski-Górecki

+ reconnect_marker_value = atoi(optarg);

atoi() isn't great, if there's garbage it just return 0, which is validated below.

Would there be value here in using strtol() which can detect input error? To at least help with maybe hard to debug issues?

I'll send v2 with this changed.

Quoted 14 lines
Quoted 14 lines
tools/vchan/vchan-socket-proxy.c +7 -0
+                if (reconnect_marker_value < 0 || reconnect_marker_value > 255) {
+                    fprintf(stderr, "invalid argument for --reconnect-marker, "
+                                    "must be a number between 0 and 255\n");
+                    usage(argv);
+                }
+                break;
             case '?':
                 usage(argv);
         }
@@ -509,6 +549,15 @@ int main(int argc, char **argv)
                 ret = 1;
                 break;
             }
+            if (reconnect_marker_value != -1) {
Quoted 8 lines

tools/vchan/vchan-socket-proxy.c int main(int argc, char **argv) · line 557

Quoted 5 lines
tools/vchan/vchan-socket-proxy.c +5 -0
+                const char marker_buf[] = { reconnect_marker_value };
+
+                if (libxenvchan_write(state.ctrl, marker_buf, sizeof(marker_buf))
+                        != sizeof(marker_buf)) {
+                    fprintf(stderr, "failed to send reconnect marker\n");

Is this an expected failure? If so, maybe adding "(ignored)" might be valuable to someone reading the logs?

No, it isn't, it may mean server is gone or other fatal communication error. The break below will terminate the process (after performing cleanup).

Quoted 15 lines

tools/vchan/vchan-socket-proxy.c int main(int argc, char **argv) · line 563

Quoted 6 lines
tools/vchan/vchan-socket-proxy.c +3 -0
+                    break;
+                }
+            }
             if (data_loop(&state) != 0)
                 break;
             /* don't reconnect if output was stdout */

Otherwise, the patch looks fine. Even if kept as is:

Thanks,

Signature · 1 line
Anthony PERARD
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab