net: stream: add a new option to automatically reconnect

In stream mode, if the server shuts down there is currently
no way to reconnect the client to a new server without removing
the NIC device and the netdev backend (or to reboot).

This patch introduces a reconnect option that specifies a delay
to try to reconnect with the same parameters.

Add a new test in qtest to test the reconnect option and the
connect/disconnect events.

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
This commit is contained in:
Laurent Vivier 2023-01-19 11:16:45 +01:00 committed by Jason Wang
parent 993f71ee33
commit 148fbf0d58
4 changed files with 162 additions and 5 deletions

View file

@ -11,6 +11,10 @@
#include <glib/gstdio.h>
#include "../unit/socket-helpers.h"
#include "libqtest.h"
#include "qapi/qmp/qstring.h"
#include "qemu/sockets.h"
#include "qapi/qobject-input-visitor.h"
#include "qapi/qapi-visit-sockets.h"
#define CONNECTION_TIMEOUT 60
@ -142,6 +146,101 @@ static void test_stream_inet_ipv4(void)
qtest_quit(qts0);
}
static void wait_stream_connected(QTestState *qts, const char *id,
SocketAddress **addr)
{
QDict *resp, *data;
QString *qstr;
QObject *obj;
Visitor *v = NULL;
resp = qtest_qmp_eventwait_ref(qts, "NETDEV_STREAM_CONNECTED");
g_assert_nonnull(resp);
data = qdict_get_qdict(resp, "data");
g_assert_nonnull(data);
qstr = qobject_to(QString, qdict_get(data, "netdev-id"));
g_assert_nonnull(data);
g_assert(!strcmp(qstring_get_str(qstr), id));
obj = qdict_get(data, "addr");
v = qobject_input_visitor_new(obj);
visit_type_SocketAddress(v, NULL, addr, NULL);
visit_free(v);
qobject_unref(resp);
}
static void wait_stream_disconnected(QTestState *qts, const char *id)
{
QDict *resp, *data;
QString *qstr;
resp = qtest_qmp_eventwait_ref(qts, "NETDEV_STREAM_DISCONNECTED");
g_assert_nonnull(resp);
data = qdict_get_qdict(resp, "data");
g_assert_nonnull(data);
qstr = qobject_to(QString, qdict_get(data, "netdev-id"));
g_assert_nonnull(data);
g_assert(!strcmp(qstring_get_str(qstr), id));
qobject_unref(resp);
}
static void test_stream_inet_reconnect(void)
{
QTestState *qts0, *qts1;
int port;
SocketAddress *addr;
port = inet_get_free_port(false);
qts0 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,server=true,addr.type=inet,"
"addr.ipv4=on,addr.ipv6=off,"
"addr.host=127.0.0.1,addr.port=%d", port);
EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
qts1 = qtest_initf("-nodefaults -M none "
"-netdev stream,server=false,id=st0,addr.type=inet,"
"addr.ipv4=on,addr.ipv6=off,reconnect=1,"
"addr.host=127.0.0.1,addr.port=%d", port);
wait_stream_connected(qts0, "st0", &addr);
g_assert_cmpint(addr->type, ==, SOCKET_ADDRESS_TYPE_INET);
g_assert_cmpstr(addr->u.inet.host, ==, "127.0.0.1");
qapi_free_SocketAddress(addr);
/* kill server */
qtest_quit(qts0);
/* check client has been disconnected */
wait_stream_disconnected(qts1, "st0");
/* restart server */
qts0 = qtest_initf("-nodefaults -M none "
"-netdev stream,id=st0,server=true,addr.type=inet,"
"addr.ipv4=on,addr.ipv6=off,"
"addr.host=127.0.0.1,addr.port=%d", port);
/* wait connection events*/
wait_stream_connected(qts0, "st0", &addr);
g_assert_cmpint(addr->type, ==, SOCKET_ADDRESS_TYPE_INET);
g_assert_cmpstr(addr->u.inet.host, ==, "127.0.0.1");
qapi_free_SocketAddress(addr);
wait_stream_connected(qts1, "st0", &addr);
g_assert_cmpint(addr->type, ==, SOCKET_ADDRESS_TYPE_INET);
g_assert_cmpstr(addr->u.inet.host, ==, "127.0.0.1");
g_assert_cmpint(atoi(addr->u.inet.port), ==, port);
qapi_free_SocketAddress(addr);
qtest_quit(qts1);
qtest_quit(qts0);
}
static void test_stream_inet_ipv6(void)
{
QTestState *qts0, *qts1;
@ -418,6 +517,8 @@ int main(int argc, char **argv)
#ifndef _WIN32
qtest_add_func("/netdev/dgram/mcast", test_dgram_mcast);
#endif
qtest_add_func("/netdev/stream/inet/reconnect",
test_stream_inet_reconnect);
}
if (has_ipv6) {
qtest_add_func("/netdev/stream/inet/ipv6", test_stream_inet_ipv6);