net/stream: deprecate 'reconnect' in favor of 'reconnect-ms'

Do the same thing we already did for chardev in c8e2b6b4d7, and
introduce a new 'reconnect-ms' option to make it possible to specify
sub-second timeouts. This also changes the related documentaion and
tests to use reconnect-ms as well.

Signed-off-by: Daniil Tatianin <d-tatianin@yandex-team.ru>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
This commit is contained in:
Daniil Tatianin 2024-10-25 10:35:24 +03:00 committed by Jason Wang
parent ae311fb315
commit c40e962d83
5 changed files with 57 additions and 26 deletions

View File

@ -400,6 +400,16 @@ Backend ``memory`` (since 9.0)
The ``reconnect`` option only allows specifiying second granularity timeouts, The ``reconnect`` option only allows specifiying second granularity timeouts,
which is not enough for all types of use cases, use ``reconnect-ms`` instead. which is not enough for all types of use cases, use ``reconnect-ms`` instead.
Net device options
''''''''''''''''''
Stream ``reconnect`` (since 9.2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The ``reconnect`` option only allows specifiying second granularity timeouts,
which is not enough for all types of use cases, use ``reconnect-ms`` instead.
CPU device properties CPU device properties
''''''''''''''''''''' '''''''''''''''''''''

View File

@ -51,7 +51,7 @@ typedef struct NetStreamState {
guint ioc_write_tag; guint ioc_write_tag;
SocketReadState rs; SocketReadState rs;
unsigned int send_index; /* number of bytes sent*/ unsigned int send_index; /* number of bytes sent*/
uint32_t reconnect; uint32_t reconnect_ms;
guint timer_tag; guint timer_tag;
SocketAddress *addr; SocketAddress *addr;
} NetStreamState; } NetStreamState;
@ -387,10 +387,9 @@ static gboolean net_stream_reconnect(gpointer data)
static void net_stream_arm_reconnect(NetStreamState *s) static void net_stream_arm_reconnect(NetStreamState *s)
{ {
if (s->reconnect && s->timer_tag == 0) { if (s->reconnect_ms && s->timer_tag == 0) {
qemu_set_info_str(&s->nc, "connecting"); qemu_set_info_str(&s->nc, "connecting");
s->timer_tag = g_timeout_add_seconds(s->reconnect, s->timer_tag = g_timeout_add(s->reconnect_ms, net_stream_reconnect, s);
net_stream_reconnect, s);
} }
} }
@ -398,7 +397,7 @@ static int net_stream_client_init(NetClientState *peer,
const char *model, const char *model,
const char *name, const char *name,
SocketAddress *addr, SocketAddress *addr,
uint32_t reconnect, uint32_t reconnect_ms,
Error **errp) Error **errp)
{ {
NetStreamState *s; NetStreamState *s;
@ -412,8 +411,8 @@ static int net_stream_client_init(NetClientState *peer,
s->ioc = QIO_CHANNEL(sioc); s->ioc = QIO_CHANNEL(sioc);
s->nc.link_down = true; s->nc.link_down = true;
s->reconnect = reconnect; s->reconnect_ms = reconnect_ms;
if (reconnect) { if (reconnect_ms) {
s->addr = QAPI_CLONE(SocketAddress, addr); s->addr = QAPI_CLONE(SocketAddress, addr);
} }
qio_channel_socket_connect_async(sioc, addr, qio_channel_socket_connect_async(sioc, addr,
@ -432,13 +431,24 @@ int net_init_stream(const Netdev *netdev, const char *name,
sock = &netdev->u.stream; sock = &netdev->u.stream;
if (!sock->has_server || !sock->server) { if (!sock->has_server || !sock->server) {
uint32_t reconnect_ms = 0;
if (sock->has_reconnect && sock->has_reconnect_ms) {
error_setg(errp, "'reconnect' and 'reconnect-ms' are mutually "
"exclusive");
return -1;
} else if (sock->has_reconnect_ms) {
reconnect_ms = sock->reconnect_ms;
} else if (sock->has_reconnect) {
reconnect_ms = sock->reconnect * 1000u;
}
return net_stream_client_init(peer, "stream", name, sock->addr, return net_stream_client_init(peer, "stream", name, sock->addr,
sock->has_reconnect ? sock->reconnect : 0, reconnect_ms, errp);
errp);
} }
if (sock->has_reconnect) { if (sock->has_reconnect || sock->has_reconnect_ms) {
error_setg(errp, "'reconnect' option is incompatible with " error_setg(errp, "'reconnect' and 'reconnect-ms' options are "
"socket in server mode"); "incompatible with socket in server mode");
return -1; return -1;
} }
return net_stream_server_init(peer, "stream", name, sock->addr, errp); return net_stream_server_init(peer, "stream", name, sock->addr, errp);

View File

@ -650,15 +650,26 @@
# attempt a reconnect after the given number of seconds. Setting # attempt a reconnect after the given number of seconds. Setting
# this to zero disables this function. (default: 0) (since 8.0) # this to zero disables this function. (default: 0) (since 8.0)
# #
# @reconnect-ms: For a client socket, if a socket is disconnected, then
# attempt a reconnect after the given number of milliseconds. Setting
# this to zero disables this function. This member is mutually
# exclusive with @reconnect. (default: 0) (Since: 9.2)
#
# Only SocketAddress types 'unix', 'inet' and 'fd' are supported. # Only SocketAddress types 'unix', 'inet' and 'fd' are supported.
# #
# Features:
#
# @deprecated: Member @reconnect is deprecated. Use @reconnect-ms
# instead.
#
# Since: 7.2 # Since: 7.2
## ##
{ 'struct': 'NetdevStreamOptions', { 'struct': 'NetdevStreamOptions',
'data': { 'data': {
'addr': 'SocketAddress', 'addr': 'SocketAddress',
'*server': 'bool', '*server': 'bool',
'*reconnect': 'uint32' } } '*reconnect': { 'type': 'int', 'features': [ 'deprecated' ] },
'*reconnect-ms': 'int' } }
## ##
# @NetdevDgramOptions: # @NetdevDgramOptions:

View File

@ -2833,9 +2833,9 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
"-netdev socket,id=str[,fd=h][,udp=host:port][,localaddr=host:port]\n" "-netdev socket,id=str[,fd=h][,udp=host:port][,localaddr=host:port]\n"
" configure a network backend to connect to another network\n" " configure a network backend to connect to another network\n"
" using an UDP tunnel\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][,reconnect=seconds]\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-ms=milliseconds]\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=unix,addr.path=path[,abstract=on|off][,tight=on|off][,reconnect-ms=milliseconds]\n"
"-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor[,reconnect=seconds]\n" "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor[,reconnect-ms=milliseconds]\n"
" configure a network backend to connect to another network\n" " configure a network backend to connect to another network\n"
" using a socket connection in stream mode.\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" "-netdev dgram,id=str,remote.type=inet,remote.host=maddr,remote.port=port[,local.type=inet,local.host=addr]\n"
@ -3291,7 +3291,7 @@ SRST
-device e1000,netdev=n1,mac=52:54:00:12:34:56 \\ -device e1000,netdev=n1,mac=52:54:00:12:34:56 \\
-netdev socket,id=n1,mcast=239.192.168.1:1102,localaddr=1.2.3.4 -netdev socket,id=n1,mcast=239.192.168.1:1102,localaddr=1.2.3.4
``-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]`` ``-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-ms=milliseconds]``
Configure a network backend to connect to another QEMU virtual machine or a proxy using a TCP/IP socket. Configure a network backend to connect to another QEMU virtual machine or a proxy using a TCP/IP socket.
``server=on|off`` ``server=on|off``
@ -3333,9 +3333,9 @@ SRST
# second VM # second VM
|qemu_system| linux.img \\ |qemu_system| linux.img \\
-device virtio-net,netdev=net0,mac=52:54:00:12:34:57 \\ -device virtio-net,netdev=net0,mac=52:54:00:12:34:57 \\
-netdev stream,id=net0,server=off,addr.type=inet,addr.host=localhost,addr.port=1234,reconnect=5 -netdev stream,id=net0,server=off,addr.type=inet,addr.host=localhost,addr.port=1234,reconnect-ms=5000
``-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path[,abstract=on|off][,tight=on|off][,reconnect=seconds]`` ``-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path[,abstract=on|off][,tight=on|off][,reconnect-ms=milliseconds]``
Configure a network backend to connect to another QEMU virtual machine or a proxy using a stream oriented unix domain socket. Configure a network backend to connect to another QEMU virtual machine or a proxy using a stream oriented unix domain socket.
``server=on|off`` ``server=on|off``
@ -3350,8 +3350,8 @@ SRST
``tight=on|off`` ``tight=on|off``
if false, pad an abstract socket address with enough null bytes to make it fill struct sockaddr_un member sun_path. if false, pad an abstract socket address with enough null bytes to make it fill struct sockaddr_un member sun_path.
``reconnect=seconds`` ``reconnect-ms=milliseconds``
for a client socket, if a socket is disconnected, then attempt a reconnect after the given number of seconds. for a client socket, if a socket is disconnected, then attempt a reconnect after the given number of milliseconds.
Setting this to zero disables this function. (default: 0) Setting this to zero disables this function. (default: 0)
Example (using passt as a replacement of -netdev user): Example (using passt as a replacement of -netdev user):
@ -3377,9 +3377,9 @@ SRST
# second VM # second VM
|qemu_system| linux.img \\ |qemu_system| linux.img \\
-device virtio-net,netdev=net0,mac=52:54:00:12:34:57 \\ -device virtio-net,netdev=net0,mac=52:54:00:12:34:57 \\
-netdev stream,id=net0,server=off,addr.type=unix,addr.path=/tmp/qemu0,reconnect=5 -netdev stream,id=net0,server=off,addr.type=unix,addr.path=/tmp/qemu0,reconnect-ms=5000
``-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor[,reconnect=seconds]`` ``-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor[,reconnect-ms=milliseconds]``
Configure a network backend to connect to another QEMU virtual machine or a proxy using a stream oriented socket file descriptor. Configure a network backend to connect to another QEMU virtual machine or a proxy using a stream oriented socket file descriptor.
``server=on|off`` ``server=on|off``
@ -4390,14 +4390,14 @@ SRST
``telnet options:`` ``telnet options:``
localhost 5555 localhost 5555
``tcp:[host]:port[,server=on|off][,wait=on|off][,nodelay=on|off][,reconnect=seconds]`` ``tcp:[host]:port[,server=on|off][,wait=on|off][,nodelay=on|off][,reconnect-ms=milliseconds]``
The TCP Net Console has two modes of operation. It can send the The TCP Net Console has two modes of operation. It can send the
serial I/O to a location or wait for a connection from a serial I/O to a location or wait for a connection from a
location. By default the TCP Net Console is sent to host at the location. By default the TCP Net Console is sent to host at the
port. If you use the ``server=on`` option QEMU will wait for a client port. If you use the ``server=on`` option QEMU will wait for a client
socket application to connect to the port before continuing, socket application to connect to the port before continuing,
unless the ``wait=on|off`` option was specified. The ``nodelay=on|off`` unless the ``wait=on|off`` option was specified. The ``nodelay=on|off``
option disables the Nagle buffering algorithm. The ``reconnect=on`` option disables the Nagle buffering algorithm. The ``reconnect-ms``
option only applies if ``server=no`` is set, if the connection goes option only applies if ``server=no`` is set, if the connection goes
down it will attempt to reconnect at the given interval. If host down it will attempt to reconnect at the given interval. If host
is omitted, 0.0.0.0 is assumed. Only one TCP connection at a is omitted, 0.0.0.0 is assumed. Only one TCP connection at a

View File

@ -204,7 +204,7 @@ static void test_stream_unix_reconnect(void)
qts1 = qtest_initf("-nodefaults -M none " qts1 = qtest_initf("-nodefaults -M none "
"-netdev stream,server=false,id=st0,addr.type=unix," "-netdev stream,server=false,id=st0,addr.type=unix,"
"addr.path=%s,reconnect=1", path); "addr.path=%s,reconnect-ms=1000", path);
wait_stream_connected(qts0, "st0", &addr); wait_stream_connected(qts0, "st0", &addr);
g_assert_cmpint(addr->type, ==, SOCKET_ADDRESS_TYPE_UNIX); g_assert_cmpint(addr->type, ==, SOCKET_ADDRESS_TYPE_UNIX);