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

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