thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-net-stream-add-a-new-option-to-automatically-reconne.patch

ed5979
From e5834364958a3914d7b8b46b985a1b054728b466 Mon Sep 17 00:00:00 2001
ed5979
From: Laurent Vivier <lvivier@redhat.com>
ed5979
Date: Thu, 19 Jan 2023 11:16:45 +0100
ed5979
Subject: [PATCH 2/8] net: stream: add a new option to automatically reconnect
ed5979
MIME-Version: 1.0
ed5979
Content-Type: text/plain; charset=UTF-8
ed5979
Content-Transfer-Encoding: 8bit
ed5979
ed5979
RH-Author: Laurent Vivier <lvivier@redhat.com>
ed5979
RH-MergeRequest: 148: net: stream: add a new option to automatically reconnect
ed5979
RH-Bugzilla: 2169232
ed5979
RH-Acked-by: Eugenio PĂ©rez <eperezma@redhat.com>
ed5979
RH-Acked-by: Cindy Lu <lulu@redhat.com>
ed5979
RH-Acked-by: MST <mst@redhat.com>
ed5979
RH-Acked-by: David Gibson (Red Hat) <dgibson@redhat.com>
ed5979
RH-Commit: [2/2] 9b87647a9ed2e7c1b91bdfa9d0a736e091c892a5 (lvivier/qemu-kvm-centos)
ed5979
ed5979
In stream mode, if the server shuts down there is currently
ed5979
no way to reconnect the client to a new server without removing
ed5979
the NIC device and the netdev backend (or to reboot).
ed5979
ed5979
This patch introduces a reconnect option that specifies a delay
ed5979
to try to reconnect with the same parameters.
ed5979
ed5979
Add a new test in qtest to test the reconnect option and the
ed5979
connect/disconnect events.
ed5979
ed5979
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
ed5979
Signed-off-by: Jason Wang <jasowang@redhat.com>
ed5979
(cherry picked from commit b95c0d4440950fba6dbef0f781962911fa42abdb)
ed5979
---
ed5979
 net/stream.c                |  53 ++++++++++++++++++-
ed5979
 qapi/net.json               |   7 ++-
ed5979
 qemu-options.hx             |   6 +--
ed5979
 tests/qtest/netdev-socket.c | 101 ++++++++++++++++++++++++++++++++++++
ed5979
 4 files changed, 162 insertions(+), 5 deletions(-)
ed5979
ed5979
diff --git a/net/stream.c b/net/stream.c
ed5979
index 37ff727e0c..9204b4c96e 100644
ed5979
--- a/net/stream.c
ed5979
+++ b/net/stream.c
ed5979
@@ -39,6 +39,8 @@
ed5979
 #include "io/channel-socket.h"
ed5979
 #include "io/net-listener.h"
ed5979
 #include "qapi/qapi-events-net.h"
ed5979
+#include "qapi/qapi-visit-sockets.h"
ed5979
+#include "qapi/clone-visitor.h"
ed5979
 
ed5979
 typedef struct NetStreamState {
ed5979
     NetClientState nc;
ed5979
@@ -49,11 +51,15 @@ typedef struct NetStreamState {
ed5979
     guint ioc_write_tag;
ed5979
     SocketReadState rs;
ed5979
     unsigned int send_index;      /* number of bytes sent*/
ed5979
+    uint32_t reconnect;
ed5979
+    guint timer_tag;
ed5979
+    SocketAddress *addr;
ed5979
 } NetStreamState;
ed5979
 
ed5979
 static void net_stream_listen(QIONetListener *listener,
ed5979
                               QIOChannelSocket *cioc,
ed5979
                               void *opaque);
ed5979
+static void net_stream_arm_reconnect(NetStreamState *s);
ed5979
 
ed5979
 static gboolean net_stream_writable(QIOChannel *ioc,
ed5979
                                     GIOCondition condition,
ed5979
@@ -170,6 +176,7 @@ static gboolean net_stream_send(QIOChannel *ioc,
ed5979
         qemu_set_info_str(&s->nc, "%s", "");
ed5979
 
ed5979
         qapi_event_send_netdev_stream_disconnected(s->nc.name);
ed5979
+        net_stream_arm_reconnect(s);
ed5979
 
ed5979
         return G_SOURCE_REMOVE;
ed5979
     }
ed5979
@@ -187,6 +194,14 @@ static gboolean net_stream_send(QIOChannel *ioc,
ed5979
 static void net_stream_cleanup(NetClientState *nc)
ed5979
 {
ed5979
     NetStreamState *s = DO_UPCAST(NetStreamState, nc, nc);
ed5979
+    if (s->timer_tag) {
ed5979
+        g_source_remove(s->timer_tag);
ed5979
+        s->timer_tag = 0;
ed5979
+    }
ed5979
+    if (s->addr) {
ed5979
+        qapi_free_SocketAddress(s->addr);
ed5979
+        s->addr = NULL;
ed5979
+    }
ed5979
     if (s->ioc) {
ed5979
         if (QIO_CHANNEL_SOCKET(s->ioc)->fd != -1) {
ed5979
             if (s->ioc_read_tag) {
ed5979
@@ -346,12 +361,37 @@ static void net_stream_client_connected(QIOTask *task, gpointer opaque)
ed5979
 error:
ed5979
     object_unref(OBJECT(s->ioc));
ed5979
     s->ioc = NULL;
ed5979
+    net_stream_arm_reconnect(s);
ed5979
+}
ed5979
+
ed5979
+static gboolean net_stream_reconnect(gpointer data)
ed5979
+{
ed5979
+    NetStreamState *s = data;
ed5979
+    QIOChannelSocket *sioc;
ed5979
+
ed5979
+    s->timer_tag = 0;
ed5979
+
ed5979
+    sioc = qio_channel_socket_new();
ed5979
+    s->ioc = QIO_CHANNEL(sioc);
ed5979
+    qio_channel_socket_connect_async(sioc, s->addr,
ed5979
+                                     net_stream_client_connected, s,
ed5979
+                                     NULL, NULL);
ed5979
+    return G_SOURCE_REMOVE;
ed5979
+}
ed5979
+
ed5979
+static void net_stream_arm_reconnect(NetStreamState *s)
ed5979
+{
ed5979
+    if (s->reconnect && s->timer_tag == 0) {
ed5979
+        s->timer_tag = g_timeout_add_seconds(s->reconnect,
ed5979
+                                             net_stream_reconnect, s);
ed5979
+    }
ed5979
 }
ed5979
 
ed5979
 static int net_stream_client_init(NetClientState *peer,
ed5979
                                   const char *model,
ed5979
                                   const char *name,
ed5979
                                   SocketAddress *addr,
ed5979
+                                  uint32_t reconnect,
ed5979
                                   Error **errp)
ed5979
 {
ed5979
     NetStreamState *s;
ed5979
@@ -364,6 +404,10 @@ static int net_stream_client_init(NetClientState *peer,
ed5979
     s->ioc = QIO_CHANNEL(sioc);
ed5979
     s->nc.link_down = true;
ed5979
 
ed5979
+    s->reconnect = reconnect;
ed5979
+    if (reconnect) {
ed5979
+        s->addr = QAPI_CLONE(SocketAddress, addr);
ed5979
+    }
ed5979
     qio_channel_socket_connect_async(sioc, addr,
ed5979
                                      net_stream_client_connected, s,
ed5979
                                      NULL, NULL);
ed5979
@@ -380,7 +424,14 @@ int net_init_stream(const Netdev *netdev, const char *name,
ed5979
     sock = &netdev->u.stream;
ed5979
 
ed5979
     if (!sock->has_server || !sock->server) {
ed5979
-        return net_stream_client_init(peer, "stream", name, sock->addr, errp);
ed5979
+        return net_stream_client_init(peer, "stream", name, sock->addr,
ed5979
+                                      sock->has_reconnect ? sock->reconnect : 0,
ed5979
+                                      errp);
ed5979
+    }
ed5979
+    if (sock->has_reconnect) {
ed5979
+        error_setg(errp, "'reconnect' option is incompatible with "
ed5979
+                         "socket in server mode");
ed5979
+        return -1;
ed5979
     }
ed5979
     return net_stream_server_init(peer, "stream", name, sock->addr, errp);
ed5979
 }
ed5979
diff --git a/qapi/net.json b/qapi/net.json
ed5979
index 522ac582ed..d6eb30008b 100644
ed5979
--- a/qapi/net.json
ed5979
+++ b/qapi/net.json
ed5979
@@ -585,6 +585,10 @@
ed5979
 # @addr: socket address to listen on (server=true)
ed5979
 #        or connect to (server=false)
ed5979
 # @server: create server socket (default: false)
ed5979
+# @reconnect: For a client socket, if a socket is disconnected,
ed5979
+#             then attempt a reconnect after the given number of seconds.
ed5979
+#             Setting this to zero disables this function. (default: 0)
ed5979
+#             (since 8.0)
ed5979
 #
ed5979
 # Only SocketAddress types 'unix', 'inet' and 'fd' are supported.
ed5979
 #
ed5979
@@ -593,7 +597,8 @@
ed5979
 { 'struct': 'NetdevStreamOptions',
ed5979
   'data': {
ed5979
     'addr':   'SocketAddress',
ed5979
-    '*server': 'bool' } }
ed5979
+    '*server': 'bool',
ed5979
+    '*reconnect': 'uint32' } }
ed5979
 
ed5979
 ##
ed5979
 # @NetdevDgramOptions:
ed5979
diff --git a/qemu-options.hx b/qemu-options.hx
ed5979
index ea02ca3a45..48eef4aa2c 100644
ed5979
--- a/qemu-options.hx
ed5979
+++ b/qemu-options.hx
ed5979
@@ -2766,9 +2766,9 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
ed5979
     "-netdev socket,id=str[,fd=h][,udp=host:port][,localaddr=host:port]\n"
ed5979
     "                configure a network backend to connect to another network\n"
ed5979
     "                using an UDP tunnel\n"
ed5979
-    "-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port[,to=maxport][,numeric=on|off][,keep-alive=on|off][,mptcp=on|off][,addr.ipv4=on|off][,addr.ipv6=on|off]\n"
ed5979
-    "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path[,abstract=on|off][,tight=on|off]\n"
ed5979
-    "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor\n"
ed5979
+    "-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port[,to=maxport][,numeric=on|off][,keep-alive=on|off][,mptcp=on|off][,addr.ipv4=on|off][,addr.ipv6=on|off][,reconnect=seconds]\n"
ed5979
+    "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path[,abstract=on|off][,tight=on|off][,reconnect=seconds]\n"
ed5979
+    "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor[,reconnect=seconds]\n"
ed5979
     "                configure a network backend to connect to another network\n"
ed5979
     "                using a socket connection in stream mode.\n"
ed5979
     "-netdev dgram,id=str,remote.type=inet,remote.host=maddr,remote.port=port[,local.type=inet,local.host=addr]\n"
ed5979
diff --git a/tests/qtest/netdev-socket.c b/tests/qtest/netdev-socket.c
ed5979
index 6ba256e173..acc32c378b 100644
ed5979
--- a/tests/qtest/netdev-socket.c
ed5979
+++ b/tests/qtest/netdev-socket.c
ed5979
@@ -11,6 +11,10 @@
ed5979
 #include <glib/gstdio.h>
ed5979
 #include "../unit/socket-helpers.h"
ed5979
 #include "libqtest.h"
ed5979
+#include "qapi/qmp/qstring.h"
ed5979
+#include "qemu/sockets.h"
ed5979
+#include "qapi/qobject-input-visitor.h"
ed5979
+#include "qapi/qapi-visit-sockets.h"
ed5979
 
ed5979
 #define CONNECTION_TIMEOUT    5
ed5979
 
ed5979
@@ -142,6 +146,101 @@ static void test_stream_inet_ipv4(void)
ed5979
     qtest_quit(qts0);
ed5979
 }
ed5979
 
ed5979
+static void wait_stream_connected(QTestState *qts, const char *id,
ed5979
+                                  SocketAddress **addr)
ed5979
+{
ed5979
+    QDict *resp, *data;
ed5979
+    QString *qstr;
ed5979
+    QObject *obj;
ed5979
+    Visitor *v = NULL;
ed5979
+
ed5979
+    resp = qtest_qmp_eventwait_ref(qts, "NETDEV_STREAM_CONNECTED");
ed5979
+    g_assert_nonnull(resp);
ed5979
+    data = qdict_get_qdict(resp, "data");
ed5979
+    g_assert_nonnull(data);
ed5979
+
ed5979
+    qstr = qobject_to(QString, qdict_get(data, "netdev-id"));
ed5979
+    g_assert_nonnull(data);
ed5979
+
ed5979
+    g_assert(!strcmp(qstring_get_str(qstr), id));
ed5979
+
ed5979
+    obj = qdict_get(data, "addr");
ed5979
+
ed5979
+    v = qobject_input_visitor_new(obj);
ed5979
+    visit_type_SocketAddress(v, NULL, addr, NULL);
ed5979
+    visit_free(v);
ed5979
+    qobject_unref(resp);
ed5979
+}
ed5979
+
ed5979
+static void wait_stream_disconnected(QTestState *qts, const char *id)
ed5979
+{
ed5979
+    QDict *resp, *data;
ed5979
+    QString *qstr;
ed5979
+
ed5979
+    resp = qtest_qmp_eventwait_ref(qts, "NETDEV_STREAM_DISCONNECTED");
ed5979
+    g_assert_nonnull(resp);
ed5979
+    data = qdict_get_qdict(resp, "data");
ed5979
+    g_assert_nonnull(data);
ed5979
+
ed5979
+    qstr = qobject_to(QString, qdict_get(data, "netdev-id"));
ed5979
+    g_assert_nonnull(data);
ed5979
+
ed5979
+    g_assert(!strcmp(qstring_get_str(qstr), id));
ed5979
+    qobject_unref(resp);
ed5979
+}
ed5979
+
ed5979
+static void test_stream_inet_reconnect(void)
ed5979
+{
ed5979
+    QTestState *qts0, *qts1;
ed5979
+    int port;
ed5979
+    SocketAddress *addr;
ed5979
+
ed5979
+    port = inet_get_free_port(false);
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,server=true,addr.type=inet,"
ed5979
+                       "addr.ipv4=on,addr.ipv6=off,"
ed5979
+                       "addr.host=127.0.0.1,addr.port=%d", port);
ed5979
+
ed5979
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
ed5979
+
ed5979
+    qts1 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,server=false,id=st0,addr.type=inet,"
ed5979
+                       "addr.ipv4=on,addr.ipv6=off,reconnect=1,"
ed5979
+                       "addr.host=127.0.0.1,addr.port=%d", port);
ed5979
+
ed5979
+    wait_stream_connected(qts0, "st0", &addr);
ed5979
+    g_assert_cmpint(addr->type, ==, SOCKET_ADDRESS_TYPE_INET);
ed5979
+    g_assert_cmpstr(addr->u.inet.host, ==, "127.0.0.1");
ed5979
+    qapi_free_SocketAddress(addr);
ed5979
+
ed5979
+    /* kill server */
ed5979
+    qtest_quit(qts0);
ed5979
+
ed5979
+    /* check client has been disconnected */
ed5979
+    wait_stream_disconnected(qts1, "st0");
ed5979
+
ed5979
+    /* restart server */
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,server=true,addr.type=inet,"
ed5979
+                       "addr.ipv4=on,addr.ipv6=off,"
ed5979
+                       "addr.host=127.0.0.1,addr.port=%d", port);
ed5979
+
ed5979
+    /* wait connection events*/
ed5979
+    wait_stream_connected(qts0, "st0", &addr);
ed5979
+    g_assert_cmpint(addr->type, ==, SOCKET_ADDRESS_TYPE_INET);
ed5979
+    g_assert_cmpstr(addr->u.inet.host, ==, "127.0.0.1");
ed5979
+    qapi_free_SocketAddress(addr);
ed5979
+
ed5979
+    wait_stream_connected(qts1, "st0", &addr);
ed5979
+    g_assert_cmpint(addr->type, ==, SOCKET_ADDRESS_TYPE_INET);
ed5979
+    g_assert_cmpstr(addr->u.inet.host, ==, "127.0.0.1");
ed5979
+    g_assert_cmpint(atoi(addr->u.inet.port), ==, port);
ed5979
+    qapi_free_SocketAddress(addr);
ed5979
+
ed5979
+    qtest_quit(qts1);
ed5979
+    qtest_quit(qts0);
ed5979
+}
ed5979
+
ed5979
 static void test_stream_inet_ipv6(void)
ed5979
 {
ed5979
     QTestState *qts0, *qts1;
ed5979
@@ -418,6 +517,8 @@ int main(int argc, char **argv)
ed5979
 #ifndef _WIN32
ed5979
         qtest_add_func("/netdev/dgram/mcast", test_dgram_mcast);
ed5979
 #endif
ed5979
+        qtest_add_func("/netdev/stream/inet/reconnect",
ed5979
+                       test_stream_inet_reconnect);
ed5979
     }
ed5979
     if (has_ipv6) {
ed5979
         qtest_add_func("/netdev/stream/inet/ipv6", test_stream_inet_ipv6);
ed5979
-- 
ed5979
2.31.1
ed5979