net: stream: add a new option to automatically reconnect
PATCH 1/1 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.
---
Notes:
Based-on: <[email protected]>
net/stream.c | 53 ++++++++++++++++++- qapi/net.json | 6 ++- qemu-options.hx | 6 +-- tests/qtest/netdev-socket.c | 100 ++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+), 5 deletions(-)
net/stream.c +52 -1
@@ -39,6 +39,8 @@#include "io/channel-socket.h" #include "io/net-listener.h" #include "qapi/qapi-events-net.h" +#include "qapi/qapi-visit-sockets.h" +#include "qapi/clone-visitor.h" typedef struct NetStreamState { NetClientState nc;
@@ -49,11 +51,15 @@ typedef struct NetStreamState {guint ioc_write_tag;SocketReadState rs;unsigned int send_index;/* number of bytes sent*/ +uint32_t reconnect; +guint timer_tag; +SocketAddress *addr; } NetStreamState;static void net_stream_listen (QIONetListener *listener,QIOChannelSocket *cioc,void *opaque); +static void net_stream_arm_reconnect (NetStreamState *s); static gboolean net_stream_writable(QIOChannel *ioc, GIOCondition condition,
@@ -170,6 +176,7 @@ static gboolean net_stream_send(QIOChannel *ioc,qemu_set_info_str (&s->nc,"" );qapi_event_send_netdev_stream_disconnected (s->nc.name); +net_stream_arm_reconnect (s);return G_SOURCE_REMOVE ; }
@@ -187,6 +194,14 @@ static gboolean net_stream_send(QIOChannel *ioc,static void net_stream_cleanup (NetClientState *nc) {NetStreamState *s =DO_UPCAST (NetStreamState, nc, nc); +if (s->timer_tag) { +g_source_remove (s->timer_tag); + s->timer_tag =0 ; + } +if (s->addr) { +qapi_free_SocketAddress (s->addr); + s->addr =NULL ; + }if (s->ioc) {if (QIO_CHANNEL_SOCKET (s->ioc)->fd !=-1 ) {if (s->ioc_read_tag) {
@@ -346,12 +361,37 @@ static void net_stream_client_connected(QIOTask *task, gpointer opaque) error:object_unref (OBJECT (s->ioc)); s->ioc =NULL ; +net_stream_arm_reconnect (s); +} + +static gboolean net_stream_reconnect (gpointer data) +{ +NetStreamState *s = data; +QIOChannelSocket *sioc; + + s->timer_tag =0 ; + + sioc =qio_channel_socket_new (); + s->ioc =QIO_CHANNEL (sioc); +qio_channel_socket_connect_async (sioc, s->addr, + net_stream_client_connected, s, +NULL ,NULL ); +return G_SOURCE_REMOVE ; +} + +static void net_stream_arm_reconnect (NetStreamState *s) +{ +if (s->reconnect && s->timer_tag ==0 ) { + s->timer_tag =g_timeout_add_seconds (s->reconnect, + net_stream_reconnect, s); + } }static int net_stream_client_init (NetClientState *peer,const char *model,const char *name,SocketAddress *addr, +uint32_t reconnect,Error **errp) {NetStreamState *s;
@@ -364,6 +404,10 @@ static int net_stream_client_init(NetClientState *peer, s->ioc =QIO_CHANNEL (sioc); s->nc.link_down = true; + s->reconnect = reconnect; +if (reconnect) { + s->addr =QAPI_CLONE (SocketAddress, addr); + }qio_channel_socket_connect_async (sioc, addr, net_stream_client_connected, s,NULL ,NULL );
@@ -380,7 +424,14 @@ int net_init_stream(const Netdev *netdev, const char *name, sock = &netdev->u.stream;if (!sock->has_server || !sock->server) { -return net_stream_client_init (peer,"stream" , name, sock->addr, errp); +return net_stream_client_init (peer,"stream" , name, sock->addr, + sock->has_reconnect ? sock->reconnect :0 , + errp); + } +if (sock->has_reconnect) { +error_setg (errp,"'reconnect' option is incompatible with " +"socket in server mode" ); +return -1 ; }return net_stream_server_init (peer,"stream" , name, sock->addr, errp); }
qapi/net.json +5 -1
@@ -585,6 +585,9 @@ # @addr: socket address to listen on (server=true) # or connect to (server=false) # @server: create server socket (default: false) +# @reconnect: For a client socket, if a socket is disconnected, +# then attempt a reconnect after the given number of seconds. +# Setting this to zero disables this function. (default: 0) # # Only SocketAddress types 'unix', 'inet' and 'fd' are supported. #
@@ -593,7 +596,8 @@ { 'struct': 'NetdevStreamOptions', 'data': { 'addr': 'SocketAddress', - '*server': 'bool' } } + '*server': 'bool', + '*reconnect': 'uint32' } } ## # @NetdevDgramOptions:
qemu-options.hx +3 -3
@@ -2766,9 +2766,9 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, "-netdev socket,id=str[,fd=h][,udp=host:port][,localaddr=host:port]\n" " configure a network backend to connect to another network\n" " using an UDP tunnel\n" - "-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" - "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path[,abstract=on|off][,tight=on|off]\n" - "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor\n" + "-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" + "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path[,abstract=on|off][,tight=on|off][,reconnect=seconds]\n" + "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor[,reconnect=seconds]\n" " configure a network backend to connect to another network\n" " using a socket connection in stream mode.\n" "-netdev dgram,id=str,remote.type=inet,remote.host=maddr,remote.port=port[,local.type=inet,local.host=addr]\n"
tests/qtest/netdev-socket.c +100 -0
118 lines not on this page · raw message
Signature · 1 line
2.38.1
Quoted 12 lines · Laurent Vivier
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 <[email protected]>---
[...]
qapi/net.json line 590
Quoted 11 lines
qapi/net.json +3
-0@@ -585,6 +585,9 @@ # @addr: socket address to listen on (server=true) # or connect to (server=false) # @server: create server socket (default: false) +# @reconnect: For a client socket, if a socket is disconnected, +# then attempt a reconnect after the given number of seconds. +# Setting this to zero disables this function. (default: 0)
Double-checking: it attempts to reconnect *once*. Correct?
Are we sure multiples of seconds will be fine?
"For a client socket": what happens for a server socket?
Quoted 13 lines
qapi/net.json +2
-1# # Only SocketAddress types 'unix', 'inet' and 'fd' are supported. #@@ -593,7 +596,8 @@ { 'struct': 'NetdevStreamOptions', 'data': { 'addr': 'SocketAddress', - '*server': 'bool' } } + '*server': 'bool', + '*reconnect': 'uint32' } } ## # @NetdevDgramOptions:
[...]
Quoted 17 lines · Markus Armbruster
Quoted 12 lines · Laurent Vivier
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 <[email protected]>---
[...]
Quoted 13 lines
qapi/net.json line 590
Quoted 11 lines
qapi/net.json +3
-0@@ -585,6 +585,9 @@ # @addr: socket address to listen on (server=true) # or connect to (server=false) # @server: create server socket (default: false) +# @reconnect: For a client socket, if a socket is disconnected, +# then attempt a reconnect after the given number of seconds. +# Setting this to zero disables this function. (default: 0)Double-checking: it attempts to reconnect *once*. Correct?
Repeatedly.
IIUC, this doesn't just try to reconnect a failed connection, it'll reconnect the initial connection if the server doesn't listen when QEMU first startup.
Quoted 1 line
Are we sure multiples of seconds will be fine?
FWIW, this text & behaviour (AFAICT) is identical to the same named option against ChardevSocket, which is good for consistency.
Since the reconnect is continuous until connected, you don't want too frequent reconnects otherwise it'll burn CPU time.
That said, if it wasn't for the pre-existing chardev option, I would have suggested milliseconds be worth considering as units. Arguably it should probaby have an increasing backoff.
We could achieve that without changing semantics in a way that would matter to apps, by declaring that 'reconnect' is the maximum reconnect time. So start a reconnect at 50ms, 100ms, 200ms, 500ms, 1s, .... $reconnect
IOW, I think its okey to set reconnect units to seconds, and we have the option to retrofit the fast start + backwoff later.
Quoted 1 line
"For a client socket": what happens for a server socket?
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|