Chardev fixes

-----BEGIN PGP SIGNATURE-----
 
 iQIcBAABAgAGBQJcZENcAAoJENro4Ql1lpzlR1IP/3VJbeI4RjOoO/Y1TLLF6y9u
 p0vvkf+HGleQ87h2fe5GPQ0r5XJ7hRA7EGPerTKES10fo+Ctvd+0/kZJPnY8B072
 JPi1qA++1dF14LtePuDJhmibirwFM2+USKjUAkAZ9D7QjhnbvJjCDnf7pIOfHB+K
 WzFVZ42yCSPAjP9PNSJgdru/xydJInxEx4HoHelLW97MF9prK0CZPl1omW5v7z25
 JCKmp1TGvxLFoyy3okZ49n675ECjogSzjnUkQxi1aw3d5jouREadvchrpka4GnYm
 txT8YiybKbM2tHc2M8Y6fL0I/cKWsyqJcTnpgU0RfVHT1HiGYpXnHbRBQWw5VFO7
 MWTCJKQ23KNXZDtvj42jaMDe+ZU4ptfsI750ZQ22f/HFAd/ox/4Lqb4n2pWac4F/
 pJ1bN8y0TO316pdlIi41l+FTF5utRzZaErBHojfCTrKZjjWvbF9qkZRXpA7KlKE7
 8dC7yoZY7QS/vP4+iy0l+dTImsKOdTCqG+sGoy8SCR8EZAPDJauUNxbRjT/qVkim
 OfkHpadS3kZEOkrVhLdLRVfZza6EHgVj3uY/FpvsCNJycQ9BpYBm8SCJN/TUDZNB
 92z2KZaYFkIEXiWP8XX5oRbPKCDZWariuMdv8XXnOnafU/Gr1Pw5flSjNzB/NPh5
 QWFqg+KEqJ4szkW5uy/n
 =I5Qi
 -----END PGP SIGNATURE-----

Merge remote-tracking branch 'remotes/elmarco/tags/chardev-pull-request' into staging

Chardev fixes

# gpg: Signature made Wed 13 Feb 2019 16:18:36 GMT
# gpg:                using RSA key DAE8E10975969CE5
# gpg: Good signature from "Marc-André Lureau <marcandre.lureau@redhat.com>" [full]
# gpg:                 aka "Marc-André Lureau <marcandre.lureau@gmail.com>" [full]
# Primary key fingerprint: 87A9 BD93 3F87 C606 D276  F62D DAE8 E109 7596 9CE5

* remotes/elmarco/tags/chardev-pull-request: (25 commits)
  char-pty: remove write_lock usage
  char-pty: remove the check for connection on write
  chardev: add a note about frontend sources and context switch
  terminal3270: do not use backend timer sources
  char: update the mux handlers in class callback
  chardev/wctablet: Fix a typo
  char: allow specifying a GMainContext at opening time
  chardev: ensure termios is fully initialized
  tests: expand coverage of socket chardev test
  chardev: fix race with client connections in tcp_chr_wait_connected
  chardev: disallow TLS/telnet/websocket with tcp_chr_wait_connected
  chardev: honour the reconnect setting in tcp_chr_wait_connected
  chardev: use a state machine for socket connection state
  chardev: split up qmp_chardev_open_socket connection code
  chardev: split tcp_chr_wait_connected into two methods
  chardev: remove unused 'sioc' variable & cleanup paths
  chardev: ensure qemu_chr_parse_compat reports missing driver error
  chardev: remove many local variables in qemu_chr_parse_socket
  chardev: forbid 'wait' option with client sockets
  chardev: forbid 'reconnect' option with server sockets
  ...

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Peter Maydell 2019-02-14 14:34:32 +00:00
commit c4c5f6573a
32 changed files with 1150 additions and 481 deletions

View File

@ -246,14 +246,15 @@ void qemu_chr_fe_deinit(CharBackend *b, bool del)
}
}
void qemu_chr_fe_set_handlers(CharBackend *b,
IOCanReadHandler *fd_can_read,
IOReadHandler *fd_read,
IOEventHandler *fd_event,
BackendChangeHandler *be_change,
void *opaque,
GMainContext *context,
bool set_open)
void qemu_chr_fe_set_handlers_full(CharBackend *b,
IOCanReadHandler *fd_can_read,
IOReadHandler *fd_read,
IOEventHandler *fd_event,
BackendChangeHandler *be_change,
void *opaque,
GMainContext *context,
bool set_open,
bool sync_state)
{
Chardev *s;
int fe_open;
@ -285,14 +286,24 @@ void qemu_chr_fe_set_handlers(CharBackend *b,
qemu_chr_fe_take_focus(b);
/* We're connecting to an already opened device, so let's make sure we
also get the open event */
if (s->be_open) {
if (sync_state && s->be_open) {
qemu_chr_be_event(s, CHR_EVENT_OPENED);
}
}
}
if (CHARDEV_IS_MUX(s)) {
mux_chr_set_handlers(s, context);
}
void qemu_chr_fe_set_handlers(CharBackend *b,
IOCanReadHandler *fd_can_read,
IOReadHandler *fd_read,
IOEventHandler *fd_event,
BackendChangeHandler *be_change,
void *opaque,
GMainContext *context,
bool set_open)
{
qemu_chr_fe_set_handlers_full(b, fd_can_read, fd_read, fd_event, be_change,
opaque, context, set_open,
true);
}
void qemu_chr_fe_take_focus(CharBackend *b)

View File

@ -278,18 +278,18 @@ static void char_mux_finalize(Object *obj)
qemu_chr_fe_deinit(&d->chr, false);
}
void mux_chr_set_handlers(Chardev *chr, GMainContext *context)
static void mux_chr_update_read_handlers(Chardev *chr)
{
MuxChardev *d = MUX_CHARDEV(chr);
/* Fix up the real driver with mux routines */
qemu_chr_fe_set_handlers(&d->chr,
mux_chr_can_read,
mux_chr_read,
mux_chr_event,
NULL,
chr,
context, true);
qemu_chr_fe_set_handlers_full(&d->chr,
mux_chr_can_read,
mux_chr_read,
mux_chr_event,
NULL,
chr,
chr->gcontext, true, false);
}
void mux_set_focus(Chardev *chr, int focus)
@ -367,7 +367,7 @@ static int open_muxes(Chardev *chr)
* mark mux as OPENED so any new FEs will immediately receive
* OPENED event
*/
qemu_chr_be_event(chr, CHR_EVENT_OPENED);
chr->be_open = 1;
return 0;
}
@ -383,6 +383,7 @@ static void char_mux_class_init(ObjectClass *oc, void *data)
cc->chr_add_watch = mux_chr_add_watch;
cc->chr_be_event = mux_chr_be_event;
cc->chr_machine_done = open_muxes;
cc->chr_update_read_handler = mux_chr_update_read_handlers;
}
static const TypeInfo char_mux_type_info = {

View File

@ -36,15 +36,12 @@ typedef struct {
QIOChannel *ioc;
int read_bytes;
/* Protected by the Chardev chr_write_lock. */
int connected;
GSource *timer_src;
GSource *open_source;
} PtyChardev;
#define PTY_CHARDEV(obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY)
static void pty_chr_update_read_handler_locked(Chardev *chr);
static void pty_chr_state(Chardev *chr, int connected);
static void pty_chr_timer_cancel(PtyChardev *s)
@ -56,32 +53,19 @@ static void pty_chr_timer_cancel(PtyChardev *s)
}
}
static void pty_chr_open_src_cancel(PtyChardev *s)
{
if (s->open_source) {
g_source_destroy(s->open_source);
g_source_unref(s->open_source);
s->open_source = NULL;
}
}
static gboolean pty_chr_timer(gpointer opaque)
{
struct Chardev *chr = CHARDEV(opaque);
PtyChardev *s = PTY_CHARDEV(opaque);
qemu_mutex_lock(&chr->chr_write_lock);
pty_chr_timer_cancel(s);
pty_chr_open_src_cancel(s);
if (!s->connected) {
/* Next poll ... */
pty_chr_update_read_handler_locked(chr);
qemu_chr_be_update_read_handlers(chr, chr->gcontext);
}
qemu_mutex_unlock(&chr->chr_write_lock);
return FALSE;
}
/* Called with chr_write_lock held. */
static void pty_chr_rearm_timer(Chardev *chr, int ms)
{
PtyChardev *s = PTY_CHARDEV(chr);
@ -94,8 +78,7 @@ static void pty_chr_rearm_timer(Chardev *chr, int ms)
g_free(name);
}
/* Called with chr_write_lock held. */
static void pty_chr_update_read_handler_locked(Chardev *chr)
static void pty_chr_update_read_handler(Chardev *chr)
{
PtyChardev *s = PTY_CHARDEV(chr);
GPollFD pfd;
@ -117,24 +100,12 @@ static void pty_chr_update_read_handler_locked(Chardev *chr)
}
}
static void pty_chr_update_read_handler(Chardev *chr)
{
qemu_mutex_lock(&chr->chr_write_lock);
pty_chr_update_read_handler_locked(chr);
qemu_mutex_unlock(&chr->chr_write_lock);
}
/* Called with chr_write_lock held. */
static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len)
{
PtyChardev *s = PTY_CHARDEV(chr);
if (!s->connected) {
/* guest sends data, check for (re-)connect */
pty_chr_update_read_handler_locked(chr);
if (!s->connected) {
return len;
}
return len;
}
return io_channel_send(s->ioc, buf, len);
}
@ -183,23 +154,11 @@ static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
return TRUE;
}
static gboolean qemu_chr_be_generic_open_func(gpointer opaque)
{
Chardev *chr = CHARDEV(opaque);
PtyChardev *s = PTY_CHARDEV(opaque);
s->open_source = NULL;
qemu_chr_be_event(chr, CHR_EVENT_OPENED);
return FALSE;
}
/* Called with chr_write_lock held. */
static void pty_chr_state(Chardev *chr, int connected)
{
PtyChardev *s = PTY_CHARDEV(chr);
if (!connected) {
pty_chr_open_src_cancel(s);
remove_fd_in_watch(chr);
s->connected = 0;
/* (re-)connect poll interval for idle guests: once per second.
@ -209,13 +168,8 @@ static void pty_chr_state(Chardev *chr, int connected)
} else {
pty_chr_timer_cancel(s);
if (!s->connected) {
g_assert(s->open_source == NULL);
s->open_source = g_idle_source_new();
s->connected = 1;
g_source_set_callback(s->open_source,
qemu_chr_be_generic_open_func,
chr, NULL);
g_source_attach(s->open_source, chr->gcontext);
qemu_chr_be_event(chr, CHR_EVENT_OPENED);
}
if (!chr->gsource) {
chr->gsource = io_add_watch_poll(chr, s->ioc,
@ -231,11 +185,9 @@ static void char_pty_finalize(Object *obj)
Chardev *chr = CHARDEV(obj);
PtyChardev *s = PTY_CHARDEV(obj);
qemu_mutex_lock(&chr->chr_write_lock);
pty_chr_state(chr, 0);
object_unref(OBJECT(s->ioc));
pty_chr_timer_cancel(s);
qemu_mutex_unlock(&chr->chr_write_lock);
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
}

View File

@ -57,7 +57,7 @@ static void qmp_chardev_open_serial(Chardev *chr,
static void tty_serial_init(int fd, int speed,
int parity, int data_bits, int stop_bits)
{
struct termios tty;
struct termios tty = {0};
speed_t spd;
#if 0

View File

@ -46,6 +46,12 @@ typedef struct {
size_t buflen;
} TCPChardevTelnetInit;
typedef enum {
TCP_CHARDEV_STATE_DISCONNECTED,
TCP_CHARDEV_STATE_CONNECTING,
TCP_CHARDEV_STATE_CONNECTED,
} TCPChardevState;
typedef struct {
Chardev parent;
QIOChannel *ioc; /* Client I/O channel */
@ -53,7 +59,7 @@ typedef struct {
QIONetListener *listener;
GSource *hup_source;
QCryptoTLSCreds *tls_creds;
int connected;
TCPChardevState state;
int max_size;
int do_telnetopt;
int do_nodelay;
@ -74,6 +80,8 @@ typedef struct {
GSource *reconnect_timer;
int64_t reconnect_time;
bool connect_err_reported;
QIOTask *connect_task;
} SocketChardev;
#define SOCKET_CHARDEV(obj) \
@ -82,6 +90,21 @@ typedef struct {
static gboolean socket_reconnect_timeout(gpointer opaque);
static void tcp_chr_telnet_init(Chardev *chr);
static void tcp_chr_change_state(SocketChardev *s, TCPChardevState state)
{
switch (state) {
case TCP_CHARDEV_STATE_DISCONNECTED:
break;
case TCP_CHARDEV_STATE_CONNECTING:
assert(s->state == TCP_CHARDEV_STATE_DISCONNECTED);
break;
case TCP_CHARDEV_STATE_CONNECTED:
assert(s->state == TCP_CHARDEV_STATE_CONNECTING);
break;
}
s->state = state;
}
static void tcp_chr_reconn_timer_cancel(SocketChardev *s)
{
if (s->reconnect_timer) {
@ -96,7 +119,8 @@ static void qemu_chr_socket_restart_timer(Chardev *chr)
SocketChardev *s = SOCKET_CHARDEV(chr);
char *name;
assert(s->connected == 0);
assert(s->state == TCP_CHARDEV_STATE_DISCONNECTED);
assert(!s->reconnect_timer);
name = g_strdup_printf("chardev-socket-reconnect-%s", chr->label);
s->reconnect_timer = qemu_chr_timeout_add_ms(chr,
s->reconnect_time * 1000,
@ -131,7 +155,7 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
{
SocketChardev *s = SOCKET_CHARDEV(chr);
if (s->connected) {
if (s->state == TCP_CHARDEV_STATE_CONNECTED) {
int ret = io_channel_send_full(s->ioc, buf, len,
s->write_msgfds,
s->write_msgfds_num);
@ -164,7 +188,7 @@ static int tcp_chr_read_poll(void *opaque)
{
Chardev *chr = CHARDEV(opaque);
SocketChardev *s = SOCKET_CHARDEV(opaque);
if (!s->connected) {
if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
return 0;
}
s->max_size = qemu_chr_be_can_write(chr);
@ -277,7 +301,7 @@ static int tcp_set_msgfds(Chardev *chr, int *fds, int num)
s->write_msgfds = NULL;
s->write_msgfds_num = 0;
if (!s->connected ||
if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
!qio_channel_has_feature(s->ioc,
QIO_CHANNEL_FEATURE_FD_PASS)) {
return -1;
@ -389,7 +413,7 @@ static void tcp_chr_free_connection(Chardev *chr)
s->ioc = NULL;
g_free(chr->filename);
chr->filename = NULL;
s->connected = 0;
tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
}
static const char *qemu_chr_socket_protocol(SocketChardev *s)
@ -442,12 +466,12 @@ static void update_disconnected_filename(SocketChardev *s)
/* NB may be called even if tcp_chr_connect has not been
* reached, due to TLS or telnet initialization failure,
* so can *not* assume s->connected == true
* so can *not* assume s->state == TCP_CHARDEV_STATE_CONNECTED
*/
static void tcp_chr_disconnect(Chardev *chr)
{
SocketChardev *s = SOCKET_CHARDEV(chr);
bool emit_close = s->connected;
bool emit_close = s->state == TCP_CHARDEV_STATE_CONNECTED;
tcp_chr_free_connection(chr);
@ -471,7 +495,8 @@ static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
uint8_t buf[CHR_READ_BUF_LEN];
int len, size;
if (!s->connected || s->max_size <= 0) {
if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
s->max_size <= 0) {
return TRUE;
}
len = sizeof(buf);
@ -508,7 +533,7 @@ static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
SocketChardev *s = SOCKET_CHARDEV(chr);
int size;
if (!s->connected) {
if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
return 0;
}
@ -564,7 +589,7 @@ static void update_ioc_handlers(SocketChardev *s)
{
Chardev *chr = CHARDEV(s);
if (!s->connected) {
if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
return;
}
@ -589,7 +614,7 @@ static void tcp_chr_connect(void *opaque)
g_free(chr->filename);
chr->filename = qemu_chr_compute_filename(s);
s->connected = 1;
tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTED);
update_ioc_handlers(s);
qemu_chr_be_event(chr, CHR_EVENT_OPENED);
}
@ -828,7 +853,7 @@ static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
{
SocketChardev *s = SOCKET_CHARDEV(chr);
if (s->ioc != NULL) {
if (s->state != TCP_CHARDEV_STATE_CONNECTING) {
return -1;
}
@ -865,11 +890,17 @@ static int tcp_chr_add_client(Chardev *chr, int fd)
{
int ret;
QIOChannelSocket *sioc;
SocketChardev *s = SOCKET_CHARDEV(chr);
if (s->state != TCP_CHARDEV_STATE_DISCONNECTED) {
return -1;
}
sioc = qio_channel_socket_new_fd(fd, NULL);
if (!sioc) {
return -1;
}
tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
tcp_chr_set_client_ioc_name(chr, sioc);
ret = tcp_chr_new_client(chr, sioc);
object_unref(OBJECT(sioc));
@ -881,35 +912,125 @@ static void tcp_chr_accept(QIONetListener *listener,
void *opaque)
{
Chardev *chr = CHARDEV(opaque);
SocketChardev *s = SOCKET_CHARDEV(chr);
tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
tcp_chr_set_client_ioc_name(chr, cioc);
tcp_chr_new_client(chr, cioc);
}
static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
static int tcp_chr_connect_client_sync(Chardev *chr, Error **errp)
{
SocketChardev *s = SOCKET_CHARDEV(chr);
QIOChannelSocket *sioc = qio_channel_socket_new();
tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
tcp_chr_set_client_ioc_name(chr, sioc);
if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
object_unref(OBJECT(sioc));
return -1;
}
tcp_chr_new_client(chr, sioc);
object_unref(OBJECT(sioc));
return 0;
}
static void tcp_chr_accept_server_sync(Chardev *chr)
{
SocketChardev *s = SOCKET_CHARDEV(chr);
QIOChannelSocket *sioc;
info_report("QEMU waiting for connection on: %s",
chr->filename);
tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
sioc = qio_net_listener_wait_client(s->listener);
tcp_chr_set_client_ioc_name(chr, sioc);
tcp_chr_new_client(chr, sioc);
object_unref(OBJECT(sioc));
}
/* It can't wait on s->connected, since it is set asynchronously
* in TLS and telnet cases, only wait for an accepted socket */
while (!s->ioc) {
static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
{
SocketChardev *s = SOCKET_CHARDEV(chr);
const char *opts[] = { "telnet", "tn3270", "websock", "tls-creds" };
bool optset[] = { s->is_telnet, s->is_tn3270, s->is_websock, s->tls_creds };
size_t i;
QEMU_BUILD_BUG_ON(G_N_ELEMENTS(opts) != G_N_ELEMENTS(optset));
for (i = 0; i < G_N_ELEMENTS(opts); i++) {
if (optset[i]) {
error_setg(errp,
"'%s' option is incompatible with waiting for "
"connection completion", opts[i]);
return -1;
}
}
tcp_chr_reconn_timer_cancel(s);
/*
* We expect states to be as follows:
*
* - server
* - wait -> CONNECTED
* - nowait -> DISCONNECTED
* - client
* - reconnect == 0 -> CONNECTED
* - reconnect != 0 -> CONNECTING
*
*/
if (s->state == TCP_CHARDEV_STATE_CONNECTING) {
if (!s->connect_task) {
error_setg(errp,
"Unexpected 'connecting' state without connect task "
"while waiting for connection completion");
return -1;
}
/*
* tcp_chr_wait_connected should only ever be run from the
* main loop thread associated with chr->gcontext, otherwise
* qio_task_wait_thread has a dangerous race condition with
* free'ing of the s->connect_task object.
*
* Acquiring the main context doesn't 100% prove we're in
* the main loop thread, but it does at least guarantee
* that the main loop won't be executed by another thread
* avoiding the race condition with the task idle callback.
*/
g_main_context_acquire(chr->gcontext);
qio_task_wait_thread(s->connect_task);
g_main_context_release(chr->gcontext);
/*
* The completion callback (qemu_chr_socket_connected) for
* s->connect_task should have set this to NULL by the time
* qio_task_wait_thread has returned.
*/
assert(!s->connect_task);
/*
* NB we are *not* guaranteed to have "s->state == ..CONNECTED"
* at this point as this first connect may be failed, so
* allow the next loop to run regardless.
*/
}
while (s->state != TCP_CHARDEV_STATE_CONNECTED) {
if (s->is_listen) {
info_report("QEMU waiting for connection on: %s",
chr->filename);
sioc = qio_net_listener_wait_client(s->listener);
tcp_chr_set_client_ioc_name(chr, sioc);
tcp_chr_new_client(chr, sioc);
object_unref(OBJECT(sioc));
tcp_chr_accept_server_sync(chr);
} else {
sioc = qio_channel_socket_new();
tcp_chr_set_client_ioc_name(chr, sioc);
if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
object_unref(OBJECT(sioc));
return -1;
Error *err = NULL;
if (tcp_chr_connect_client_sync(chr, &err) < 0) {
if (s->reconnect_time) {
error_free(err);
g_usleep(s->reconnect_time * 1000ULL * 1000ULL);
} else {
error_propagate(errp, err);
return -1;
}
}
tcp_chr_new_client(chr, sioc);
object_unref(OBJECT(sioc));
}
}
@ -945,7 +1066,10 @@ static void qemu_chr_socket_connected(QIOTask *task, void *opaque)
SocketChardev *s = SOCKET_CHARDEV(chr);
Error *err = NULL;
s->connect_task = NULL;
if (qio_task_propagate_error(task, &err)) {
tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
check_report_connect_error(chr, err);
error_free(err);
goto cleanup;
@ -958,16 +1082,45 @@ cleanup:
object_unref(OBJECT(sioc));
}
static void tcp_chr_connect_async(Chardev *chr)
static void tcp_chr_connect_client_task(QIOTask *task,
gpointer opaque)
{
QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
SocketAddress *addr = opaque;
Error *err = NULL;
qio_channel_socket_connect_sync(ioc, addr, &err);
qio_task_set_error(task, err);
}
static void tcp_chr_connect_client_async(Chardev *chr)
{
SocketChardev *s = SOCKET_CHARDEV(chr);
QIOChannelSocket *sioc;
tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
sioc = qio_channel_socket_new();
tcp_chr_set_client_ioc_name(chr, sioc);
qio_channel_socket_connect_async(sioc, s->addr,
qemu_chr_socket_connected,
chr, NULL, chr->gcontext);
/*
* Normally code would use the qio_channel_socket_connect_async
* method which uses a QIOTask + qio_task_set_error internally
* to avoid blocking. The tcp_chr_wait_connected method, however,
* needs a way to synchronize with completion of the background
* connect task which can't be done with the QIOChannelSocket
* async APIs. Thus we must use QIOTask directly to implement
* the non-blocking concept locally.
*/
s->connect_task = qio_task_new(OBJECT(sioc),
qemu_chr_socket_connected,
chr, NULL);
qio_task_run_in_thread(s->connect_task,
tcp_chr_connect_client_task,
s->addr,
NULL,
chr->gcontext);
}
static gboolean socket_reconnect_timeout(gpointer opaque)
@ -982,11 +1135,138 @@ static gboolean socket_reconnect_timeout(gpointer opaque)
return false;
}
tcp_chr_connect_async(chr);
tcp_chr_connect_client_async(chr);
return false;
}
static int qmp_chardev_open_socket_server(Chardev *chr,
bool is_telnet,
bool is_waitconnect,
Error **errp)
{
SocketChardev *s = SOCKET_CHARDEV(chr);
char *name;
if (is_telnet) {
s->do_telnetopt = 1;
}
s->listener = qio_net_listener_new();
name = g_strdup_printf("chardev-tcp-listener-%s", chr->label);
qio_net_listener_set_name(s->listener, name);
g_free(name);
if (qio_net_listener_open_sync(s->listener, s->addr, errp) < 0) {
object_unref(OBJECT(s->listener));
s->listener = NULL;
return -1;
}
qapi_free_SocketAddress(s->addr);
s->addr = socket_local_address(s->listener->sioc[0]->fd, errp);
update_disconnected_filename(s);
if (is_waitconnect) {
tcp_chr_accept_server_sync(chr);
} else {
qio_net_listener_set_client_func_full(s->listener,
tcp_chr_accept,
chr, NULL,
chr->gcontext);
}
return 0;
}
static int qmp_chardev_open_socket_client(Chardev *chr,
int64_t reconnect,
Error **errp)
{
SocketChardev *s = SOCKET_CHARDEV(chr);
if (reconnect > 0) {
s->reconnect_time = reconnect;
tcp_chr_connect_client_async(chr);
return 0;
} else {
return tcp_chr_connect_client_sync(chr, errp);
}
}
static bool qmp_chardev_validate_socket(ChardevSocket *sock,
SocketAddress *addr,
Error **errp)
{
/* Validate any options which have a dependency on address type */
switch (addr->type) {
case SOCKET_ADDRESS_TYPE_FD:
if (sock->has_reconnect) {
error_setg(errp,
"'reconnect' option is incompatible with "
"'fd' address type");
return false;
}
if (sock->has_tls_creds &&
!(sock->has_server && sock->server)) {
error_setg(errp,
"'tls_creds' option is incompatible with "
"'fd' address type as client");
return false;
}
break;
case SOCKET_ADDRESS_TYPE_UNIX:
if (sock->has_tls_creds) {
error_setg(errp,
"'tls_creds' option is incompatible with "
"'unix' address type");
return false;
}
break;
case SOCKET_ADDRESS_TYPE_INET:
break;
case SOCKET_ADDRESS_TYPE_VSOCK:
if (sock->has_tls_creds) {
error_setg(errp,
"'tls_creds' option is incompatible with "
"'vsock' address type");
return false;
}
default:
break;
}
/* Validate any options which have a dependancy on client vs server */
if (!sock->has_server || sock->server) {
if (sock->has_reconnect) {
error_setg(errp,
"'reconnect' option is incompatible with "
"socket in server listen mode");
return false;
}
} else {
if (sock->has_websocket && sock->websocket) {
error_setg(errp, "%s", "Websocket client is not implemented");
return false;
}
if (sock->has_wait) {
error_setg(errp, "%s",
"'wait' option is incompatible with "
"socket in client connect mode");
return false;
}
}
return true;
}
static void qmp_chardev_open_socket(Chardev *chr,
ChardevBackend *backend,
bool *be_opened,
@ -1001,14 +1281,8 @@ static void qmp_chardev_open_socket(Chardev *chr,
bool is_waitconnect = sock->has_wait ? sock->wait : false;
bool is_websock = sock->has_websocket ? sock->websocket : false;
int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
QIOChannelSocket *sioc = NULL;
SocketAddress *addr;
if (!is_listen && is_websock) {
error_setg(errp, "%s", "Websocket client is not implemented");
goto error;
}
s->is_listen = is_listen;
s->is_telnet = is_telnet;
s->is_tn3270 = is_tn3270;
@ -1021,7 +1295,7 @@ static void qmp_chardev_open_socket(Chardev *chr,
if (!creds) {
error_setg(errp, "No TLS credentials with id '%s'",
sock->tls_creds);
goto error;
return;
}
s->tls_creds = (QCryptoTLSCreds *)
object_dynamic_cast(creds,
@ -1029,30 +1303,30 @@ static void qmp_chardev_open_socket(Chardev *chr,
if (!s->tls_creds) {
error_setg(errp, "Object with id '%s' is not TLS credentials",
sock->tls_creds);
goto error;
return;
}
object_ref(OBJECT(s->tls_creds));
if (is_listen) {
if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
error_setg(errp, "%s",
"Expected TLS credentials for server endpoint");
goto error;
return;
}
} else {
if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
error_setg(errp, "%s",
"Expected TLS credentials for client endpoint");
goto error;
return;
}
}
}
s->addr = addr = socket_address_flatten(sock->addr);
if (sock->has_reconnect && addr->type == SOCKET_ADDRESS_TYPE_FD) {
error_setg(errp, "'reconnect' option is incompatible with 'fd'");
goto error;
if (!qmp_chardev_validate_socket(sock, addr, errp)) {
return;
}
qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
/* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
@ -1064,73 +1338,25 @@ static void qmp_chardev_open_socket(Chardev *chr,
update_disconnected_filename(s);
if (is_listen) {
if (is_telnet || is_tn3270) {
s->do_telnetopt = 1;
if (s->is_listen) {
if (qmp_chardev_open_socket_server(chr, is_telnet || is_tn3270,
is_waitconnect, errp) < 0) {
return;
}
} else if (reconnect > 0) {
s->reconnect_time = reconnect;
}
if (s->reconnect_time) {
tcp_chr_connect_async(chr);
} else {
if (s->is_listen) {
char *name;
s->listener = qio_net_listener_new();
name = g_strdup_printf("chardev-tcp-listener-%s", chr->label);
qio_net_listener_set_name(s->listener, name);
g_free(name);
if (qio_net_listener_open_sync(s->listener, s->addr, errp) < 0) {
object_unref(OBJECT(s->listener));
s->listener = NULL;
goto error;
}
qapi_free_SocketAddress(s->addr);
s->addr = socket_local_address(s->listener->sioc[0]->fd, errp);
update_disconnected_filename(s);
if (is_waitconnect &&
qemu_chr_wait_connected(chr, errp) < 0) {
return;
}
if (!s->ioc) {
qio_net_listener_set_client_func_full(s->listener,
tcp_chr_accept,
chr, NULL,
chr->gcontext);
}
} else if (qemu_chr_wait_connected(chr, errp) < 0) {
goto error;
if (qmp_chardev_open_socket_client(chr, reconnect, errp) < 0) {
return;
}
}
return;
error:
if (sioc) {
object_unref(OBJECT(sioc));
}
}
static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
Error **errp)
{
bool is_listen = qemu_opt_get_bool(opts, "server", false);
bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
bool is_tn3270 = qemu_opt_get_bool(opts, "tn3270", false);
bool is_websock = qemu_opt_get_bool(opts, "websocket", false);
bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
const char *path = qemu_opt_get(opts, "path");
const char *host = qemu_opt_get(opts, "host");
const char *port = qemu_opt_get(opts, "port");
const char *fd = qemu_opt_get(opts, "fd");
const char *tls_creds = qemu_opt_get(opts, "tls-creds");
SocketAddressLegacy *addr;
ChardevSocket *sock;
@ -1140,45 +1366,39 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
return;
}
backend->type = CHARDEV_BACKEND_KIND_SOCKET;
if (path) {
if (tls_creds) {
error_setg(errp, "TLS can only be used over TCP socket");
return;
}
} else if (host) {
if (!port) {
error_setg(errp, "chardev: socket: no port given");
return;
}
} else if (fd) {
/* We don't know what host to validate against when in client mode */
if (tls_creds && !is_listen) {
error_setg(errp, "TLS can not be used with pre-opened client FD");
return;
}
} else {
g_assert_not_reached();
if (host && !port) {
error_setg(errp, "chardev: socket: no port given");
return;
}
backend->type = CHARDEV_BACKEND_KIND_SOCKET;
sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock));
sock->has_nodelay = true;
sock->nodelay = do_nodelay;
sock->has_nodelay = qemu_opt_get(opts, "delay");
sock->nodelay = !qemu_opt_get_bool(opts, "delay", true);
/*
* We have different default to QMP for 'server', hence
* we can't just check for existence of 'server'
*/
sock->has_server = true;
sock->server = is_listen;
sock->has_telnet = true;
sock->telnet = is_telnet;
sock->has_tn3270 = true;
sock->tn3270 = is_tn3270;
sock->has_websocket = true;
sock->websocket = is_websock;
sock->has_wait = true;
sock->wait = is_waitconnect;
sock->server = qemu_opt_get_bool(opts, "server", false);
sock->has_telnet = qemu_opt_get(opts, "telnet");
sock->telnet = qemu_opt_get_bool(opts, "telnet", false);
sock->has_tn3270 = qemu_opt_get(opts, "tn3270");
sock->tn3270 = qemu_opt_get_bool(opts, "tn3270", false);
sock->has_websocket = qemu_opt_get(opts, "websocket");
sock->websocket = qemu_opt_get_bool(opts, "websocket", false);
/*
* We have different default to QMP for 'wait' when 'server'
* is set, hence we can't just check for existence of 'wait'
*/
sock->has_wait = qemu_opt_find(opts, "wait") || sock->server;
sock->wait = qemu_opt_get_bool(opts, "wait", true);
sock->has_reconnect = qemu_opt_find(opts, "reconnect");
sock->reconnect = reconnect;
sock->tls_creds = g_strdup(tls_creds);
sock->reconnect = qemu_opt_get_number(opts, "reconnect", 0);
sock->has_tls_creds = qemu_opt_get(opts, "tls-creds");
sock->tls_creds = g_strdup(qemu_opt_get(opts, "tls-creds"));
addr = g_new0(SocketAddressLegacy, 1);
if (path) {
@ -1223,7 +1443,7 @@ char_socket_get_connected(Object *obj, Error **errp)
{
SocketChardev *s = SOCKET_CHARDEV(obj);
return s->connected;
return s->state == TCP_CHARDEV_STATE_CONNECTED;
}
static void char_socket_class_init(ObjectClass *oc, void *data)

View File

@ -490,6 +490,8 @@ QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,
return opts;
}
error_report("'%s' is not a valid char driver", filename);
fail:
qemu_opts_del(opts);
return NULL;
@ -634,7 +636,8 @@ ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts, Error **errp)
return backend;
}
Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)
Chardev *qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
Error **errp)
{
const ChardevClass *cc;
Chardev *chr = NULL;
@ -674,7 +677,7 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)
chr = qemu_chardev_new(bid ? bid : id,
object_class_get_name(OBJECT_CLASS(cc)),
backend, errp);
backend, context, errp);
if (chr == NULL) {
goto out;
@ -687,7 +690,7 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts, Error **errp)
backend->type = CHARDEV_BACKEND_KIND_MUX;
backend->u.mux.data = g_new0(ChardevMux, 1);
backend->u.mux.data->chardev = g_strdup(bid);
mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, errp);
mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, context, errp);
if (mux == NULL) {
object_unparent(OBJECT(chr));
chr = NULL;
@ -703,7 +706,7 @@ out:
}
Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
bool permit_mux_mon)
bool permit_mux_mon, GMainContext *context)
{
const char *p;
Chardev *chr;
@ -718,7 +721,7 @@ Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
if (!opts)
return NULL;
chr = qemu_chr_new_from_opts(opts, &err);
chr = qemu_chr_new_from_opts(opts, context, &err);
if (!chr) {
error_report_err(err);
goto out;
@ -736,10 +739,11 @@ out:
static Chardev *qemu_chr_new_permit_mux_mon(const char *label,
const char *filename,
bool permit_mux_mon)
bool permit_mux_mon,
GMainContext *context)
{
Chardev *chr;
chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon);
chr = qemu_chr_new_noreplay(label, filename, permit_mux_mon, context);
if (chr) {
if (replay_mode != REPLAY_MODE_NONE) {
qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_REPLAY);
@ -753,14 +757,16 @@ static Chardev *qemu_chr_new_permit_mux_mon(const char *label,
return chr;
}
Chardev *qemu_chr_new(const char *label, const char *filename)
Chardev *qemu_chr_new(const char *label, const char *filename,
GMainContext *context)
{
return qemu_chr_new_permit_mux_mon(label, filename, false);
return qemu_chr_new_permit_mux_mon(label, filename, false, context);
}
Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename)
Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename,
GMainContext *context)
{
return qemu_chr_new_permit_mux_mon(label, filename, true);
return qemu_chr_new_permit_mux_mon(label, filename, true, context);
}
static int qmp_query_chardev_foreach(Object *obj, void *data)
@ -935,6 +941,7 @@ void qemu_chr_set_feature(Chardev *chr,
Chardev *qemu_chardev_new(const char *id, const char *typename,
ChardevBackend *backend,
GMainContext *gcontext,
Error **errp)
{
Object *obj;
@ -947,6 +954,7 @@ Chardev *qemu_chardev_new(const char *id, const char *typename,
obj = object_new(typename);
chr = CHARDEV(obj);
chr->label = g_strdup(id);
chr->gcontext = gcontext;
qemu_char_open(chr, backend, &be_opened, &local_err);
if (local_err) {
@ -991,7 +999,7 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
}
chr = qemu_chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
backend, errp);
backend, NULL, errp);
if (!chr) {
return NULL;
}
@ -1049,7 +1057,7 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
}
chr_new = qemu_chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)),
backend, errp);
backend, chr->gcontext, errp);
if (!chr_new) {
return NULL;
}

View File

@ -177,7 +177,7 @@ static void wctablet_input_sync(DeviceState *dev)
}
static QemuInputHandler wctablet_handler = {
.name = "QEMU Wacome Pen Tablet",
.name = "QEMU Wacom Pen Tablet",
.mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
.event = wctablet_input_event,
.sync = wctablet_input_sync,

View File

@ -2531,7 +2531,7 @@ int gdbserver_start(const char *device)
* FIXME: it's a bit weird to allow using a mux chardev here
* and implicitly setup a monitor. We may want to break this.
*/
chr = qemu_chr_new_noreplay("gdb", device, true);
chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
if (!chr)
return -1;
}
@ -2545,7 +2545,7 @@ int gdbserver_start(const char *device)
/* Initialize a monitor terminal for gdb */
mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
NULL, &error_abort);
NULL, NULL, &error_abort);
monitor_init(mon_chr, 0);
} else {
qemu_chr_fe_deinit(&s->chr, true);

2
hmp.c
View File

@ -2395,7 +2395,7 @@ void hmp_chardev_add(Monitor *mon, const QDict *qdict)
if (opts == NULL) {
error_setg(&err, "Parsing chardev args failed");
} else {
qemu_chr_new_from_opts(opts, &err);
qemu_chr_new_from_opts(opts, NULL, &err);
qemu_opts_del(opts);
}
hmp_handle_error(mon, &err);

View File

@ -799,7 +799,7 @@ static struct omap_sti_s *omap_sti_init(struct omap_target_agent_s *ta,
s->irq = irq;
omap_sti_reset(s);
qemu_chr_fe_init(&s->chr, chr ?: qemu_chr_new("null", "null"),
qemu_chr_fe_init(&s->chr, chr ?: qemu_chr_new("null", "null", NULL),
&error_abort);
memory_region_init_io(&s->iomem, NULL, &omap_sti_ops, s, "omap.sti",

View File

@ -501,7 +501,7 @@ static const TypeInfo char_hci_type_info = {
Chardev *uart_hci_init(void)
{
return qemu_chardev_new(NULL, TYPE_CHARDEV_HCI,
NULL, &error_abort);
NULL, NULL, &error_abort);
}
static void register_types(void)

View File

@ -63,7 +63,7 @@ struct omap_uart_s *omap_uart_init(hwaddr base,
s->irq = irq;
s->serial = serial_mm_init(get_system_memory(), base, 2, irq,
omap_clk_getrate(fclk)/16,
chr ?: qemu_chr_new(label, "null"),
chr ?: qemu_chr_new(label, "null", NULL),
DEVICE_NATIVE_ENDIAN);
return s;
}
@ -183,6 +183,6 @@ void omap_uart_attach(struct omap_uart_s *s, Chardev *chr)
/* TODO: Should reuse or destroy current s->serial */
s->serial = serial_mm_init(get_system_memory(), s->base, 2, s->irq,
omap_clk_getrate(s->fclk) / 16,
chr ?: qemu_chr_new("null", "null"),
chr ?: qemu_chr_new("null", "null", NULL),
DEVICE_NATIVE_ENDIAN);
}

View File

@ -31,7 +31,7 @@ typedef struct Terminal3270 {
uint8_t outv[OUTPUT_BUFFER_SIZE];
int in_len;
bool handshake_done;
GSource *timer_src;
guint timer_tag;
} Terminal3270;
#define TYPE_TERMINAL_3270 "x-terminal3270"
@ -47,10 +47,9 @@ static int terminal_can_read(void *opaque)
static void terminal_timer_cancel(Terminal3270 *t)
{
if (t->timer_src) {
g_source_destroy(t->timer_src);
g_source_unref(t->timer_src);
t->timer_src = NULL;
if (t->timer_tag) {
g_source_remove(t->timer_tag);
t->timer_tag = 0;
}
}
@ -100,8 +99,7 @@ static void terminal_read(void *opaque, const uint8_t *buf, int size)
assert(size <= (INPUT_BUFFER_SIZE - t->in_len));
terminal_timer_cancel(t);
t->timer_src = qemu_chr_timeout_add_ms(t->chr.chr, 600 * 1000,
send_timing_mark_cb, t);
t->timer_tag = g_timeout_add_seconds(600, send_timing_mark_cb, t);
memcpy(&t->inv[t->in_len], buf, size);
t->in_len += size;
if (t->in_len < 2) {
@ -160,8 +158,7 @@ static void chr_event(void *opaque, int event)
* char-socket.c. Once qemu receives the terminal-type of the
* client, mark handshake done and trigger everything rolling again.
*/
t->timer_src = qemu_chr_timeout_add_ms(t->chr.chr, 600 * 1000,
send_timing_mark_cb, t);
t->timer_tag = g_timeout_add_seconds(600, send_timing_mark_cb, t);
break;
case CHR_EVENT_CLOSED:
sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END;

View File

@ -211,7 +211,8 @@ static int con_init(struct XenLegacyDevice *xendev)
* FIXME: sure we want to support implicit
* muxed monitors here?
*/
qemu_chr_new_mux_mon(label, output), &error_abort);
qemu_chr_new_mux_mon(label, output, NULL),
&error_abort);
}
xenstore_store_pv_console_info(con->xendev.dev,

View File

@ -44,7 +44,7 @@ static void isa_superio_realize(DeviceState *dev, Error **errp)
chr = parallel_hds[i];
if (chr == NULL) {
name = g_strdup_printf("discarding-parallel%d", i);
chr = qemu_chr_new(name, "null");
chr = qemu_chr_new(name, "null", NULL);
} else {
name = g_strdup_printf("parallel%d", i);
}
@ -84,7 +84,7 @@ static void isa_superio_realize(DeviceState *dev, Error **errp)
chr = serial_hd(i);
if (chr == NULL) {
name = g_strdup_printf("discarding-serial%d", i);
chr = qemu_chr_new(name, "null");
chr = qemu_chr_new(name, "null", NULL);
} else {
name = g_strdup_printf("serial%d", i);
}

View File

@ -512,7 +512,7 @@ static void boston_mach_init(MachineState *machine)
memory_region_init_io(lcd, NULL, &boston_lcd_ops, s, "boston-lcd", 0x8);
memory_region_add_subregion_overlap(sys_mem, 0x17fff000, lcd, 0);
chr = qemu_chr_new("lcd", "vc:320x240");
chr = qemu_chr_new("lcd", "vc:320x240", NULL);
qemu_chr_fe_init(&s->lcd_display, chr, NULL);
qemu_chr_fe_set_handlers(&s->lcd_display, NULL, NULL,
boston_lcd_event, NULL, s, NULL, true);

View File

@ -568,7 +568,7 @@ static MaltaFPGAState *malta_fpga_init(MemoryRegion *address_space,
memory_region_add_subregion(address_space, base, &s->iomem_lo);
memory_region_add_subregion(address_space, base + 0xa00, &s->iomem_hi);
chr = qemu_chr_new("fpga", "vc:320x200");
chr = qemu_chr_new("fpga", "vc:320x200", NULL);
qemu_chr_fe_init(&s->display, chr, NULL);
qemu_chr_fe_set_handlers(&s->display, NULL, NULL,
malta_fgpa_display_event, NULL, s, NULL, true);

View File

@ -514,7 +514,7 @@ static USBDevice *usb_braille_init(USBBus *bus, const char *unused)
USBDevice *dev;
Chardev *cdrv;
cdrv = qemu_chr_new("braille", "braille");
cdrv = qemu_chr_new("braille", "braille", NULL);
if (!cdrv)
return NULL;

View File

@ -67,7 +67,7 @@ bool qemu_chr_fe_backend_connected(CharBackend *be);
bool qemu_chr_fe_backend_open(CharBackend *be);
/**
* qemu_chr_fe_set_handlers:
* qemu_chr_fe_set_handlers_full:
* @b: a CharBackend
* @fd_can_read: callback to get the amount of data the frontend may
* receive
@ -79,12 +79,28 @@ bool qemu_chr_fe_backend_open(CharBackend *be);
* @context: a main loop context or NULL for the default
* @set_open: whether to call qemu_chr_fe_set_open() implicitely when
* any of the handler is non-NULL
* @sync_state: whether to issue event callback with updated state
*
* Set the front end char handlers. The front end takes the focus if
* any of the handler is non-NULL.
*
* Without associated Chardev, nothing is changed.
*/
void qemu_chr_fe_set_handlers_full(CharBackend *b,
IOCanReadHandler *fd_can_read,
IOReadHandler *fd_read,
IOEventHandler *fd_event,
BackendChangeHandler *be_change,
void *opaque,
GMainContext *context,
bool set_open,
bool sync_state);
/**
* qemu_chr_fe_set_handlers:
*
* Version of qemu_chr_fe_set_handlers_full() with sync_state = true.
*/
void qemu_chr_fe_set_handlers(CharBackend *b,
IOCanReadHandler *fd_can_read,
IOReadHandler *fd_read,
@ -168,6 +184,9 @@ void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
* is active; return the #GSource's tag. If it is disconnected,
* or without associated Chardev, return 0.
*
* Note that you are responsible to update the front-end sources if
* you are switching the main context with qemu_chr_fe_set_handlers().
*
* Returns: the source tag
*/
guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,

View File

@ -55,7 +55,6 @@ typedef struct MuxChardev {
#define CHARDEV_IS_MUX(chr) \
object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX)
void mux_chr_set_handlers(Chardev *chr, GMainContext *context);
void mux_set_focus(Chardev *chr, int focus);
void mux_chr_send_all_event(Chardev *chr, int event);

View File

@ -73,6 +73,7 @@ struct Chardev {
/**
* qemu_chr_new_from_opts:
* @opts: see qemu-config.c for a list of valid options
* @context: the #GMainContext to be used at initialization time
*
* Create a new character backend from a QemuOpts list.
*
@ -81,6 +82,7 @@ struct Chardev {
* or left untouched in case of help option
*/
Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
GMainContext *context,
Error **errp);
/**
@ -106,25 +108,29 @@ ChardevBackend *qemu_chr_parse_opts(QemuOpts *opts,
* qemu_chr_new:
* @label: the name of the backend
* @filename: the URI
* @context: the #GMainContext to be used at initialization time
*
* Create a new character backend from a URI.
* Do not implicitly initialize a monitor if the chardev is muxed.
*
* Returns: a new character backend
*/
Chardev *qemu_chr_new(const char *label, const char *filename);
Chardev *qemu_chr_new(const char *label, const char *filename,
GMainContext *context);
/**
* qemu_chr_new_mux_mon:
* @label: the name of the backend
* @filename: the URI
* @context: the #GMainContext to be used at initialization time
*
* Create a new character backend from a URI.
* Implicitly initialize a monitor if the chardev is muxed.
*
* Returns: a new character backend
*/
Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename);
Chardev *qemu_chr_new_mux_mon(const char *label, const char *filename,
GMainContext *context);
/**
* qemu_chr_change:
@ -146,6 +152,7 @@ void qemu_chr_cleanup(void);
* @label: the name of the backend
* @filename: the URI
* @permit_mux_mon: if chardev is muxed, initialize a monitor
* @context: the #GMainContext to be used at initialization time
*
* Create a new character backend from a URI.
* Character device communications are not written
@ -154,7 +161,7 @@ void qemu_chr_cleanup(void);
* Returns: a new character backend
*/
Chardev *qemu_chr_new_noreplay(const char *label, const char *filename,
bool permit_mux_mon);
bool permit_mux_mon, GMainContext *context);
/**
* qemu_chr_be_can_write:
@ -272,7 +279,8 @@ typedef struct ChardevClass {
} ChardevClass;
Chardev *qemu_chardev_new(const char *id, const char *typename,
ChardevBackend *backend, Error **errp);
ChardevBackend *backend, GMainContext *context,
Error **errp);
extern int term_escape_char;

View File

@ -232,7 +232,8 @@ QIOTask *qio_task_new(Object *source,
*
* Run a task in a background thread. When @worker
* returns it will call qio_task_complete() in
* the event thread context that provided.
* the thread that is running the main loop associated
* with @context.
*/
void qio_task_run_in_thread(QIOTask *task,
QIOTaskWorker worker,
@ -240,6 +241,32 @@ void qio_task_run_in_thread(QIOTask *task,
GDestroyNotify destroy,
GMainContext *context);
/**
* qio_task_wait_thread:
* @task: the task struct
*
* Wait for completion of a task that was previously
* invoked using qio_task_run_in_thread. This MUST
* ONLY be invoked if the task has not already
* completed, since after the completion callback
* is invoked, @task will have been freed.
*
* To avoid racing with execution of the completion
* callback provided with qio_task_new, this method
* MUST ONLY be invoked from the thread that is
* running the main loop associated with @context
* parameter to qio_task_run_in_thread.
*
* When the thread has completed, the completion
* callback provided to qio_task_new will be invoked.
* When that callback returns @task will be freed,
* so @task must not be referenced after this
* method completes.
*/
void qio_task_wait_thread(QIOTask *task);
/**
* qio_task_complete:
* @task: the task struct

101
io/task.c
View File

@ -24,6 +24,15 @@
#include "qemu/thread.h"
#include "trace.h"
struct QIOTaskThreadData {
QIOTaskWorker worker;
gpointer opaque;
GDestroyNotify destroy;
GMainContext *context;
GSource *completion;
};
struct QIOTask {
Object *source;
QIOTaskFunc func;
@ -32,6 +41,9 @@ struct QIOTask {
Error *err;
gpointer result;
GDestroyNotify destroyResult;
QemuMutex thread_lock;
QemuCond thread_cond;
struct QIOTaskThreadData *thread;
};
@ -49,6 +61,8 @@ QIOTask *qio_task_new(Object *source,
task->func = func;
task->opaque = opaque;
task->destroy = destroy;
qemu_mutex_init(&task->thread_lock);
qemu_cond_init(&task->thread_cond);
trace_qio_task_new(task, source, func, opaque);
@ -57,6 +71,19 @@ QIOTask *qio_task_new(Object *source,
static void qio_task_free(QIOTask *task)
{
qemu_mutex_lock(&task->thread_lock);
if (task->thread) {
if (task->thread->destroy) {
task->thread->destroy(task->thread->opaque);
}
if (task->thread->context) {
g_main_context_unref(task->thread->context);
}
g_free(task->thread);
}
if (task->destroy) {
task->destroy(task->opaque);
}
@ -68,35 +95,20 @@ static void qio_task_free(QIOTask *task)
}
object_unref(task->source);
qemu_mutex_unlock(&task->thread_lock);
qemu_mutex_destroy(&task->thread_lock);
qemu_cond_destroy(&task->thread_cond);
g_free(task);
}
struct QIOTaskThreadData {
QIOTask *task;
QIOTaskWorker worker;
gpointer opaque;
GDestroyNotify destroy;
GMainContext *context;
};
static gboolean qio_task_thread_result(gpointer opaque)
{
struct QIOTaskThreadData *data = opaque;
QIOTask *task = opaque;
trace_qio_task_thread_result(data->task);
qio_task_complete(data->task);
if (data->destroy) {
data->destroy(data->opaque);
}
if (data->context) {
g_main_context_unref(data->context);
}
g_free(data);
trace_qio_task_thread_result(task);
qio_task_complete(task);
return FALSE;
}
@ -104,22 +116,30 @@ static gboolean qio_task_thread_result(gpointer opaque)
static gpointer qio_task_thread_worker(gpointer opaque)
{
struct QIOTaskThreadData *data = opaque;
GSource *idle;
QIOTask *task = opaque;
trace_qio_task_thread_run(data->task);
data->worker(data->task, data->opaque);
trace_qio_task_thread_run(task);
task->thread->worker(task, task->thread->opaque);
/* We're running in the background thread, and must only
* ever report the task results in the main event loop
* thread. So we schedule an idle callback to report
* the worker results
*/
trace_qio_task_thread_exit(data->task);
trace_qio_task_thread_exit(task);
idle = g_idle_source_new();
g_source_set_callback(idle, qio_task_thread_result, data, NULL);
g_source_attach(idle, data->context);
qemu_mutex_lock(&task->thread_lock);
task->thread->completion = g_idle_source_new();
g_source_set_callback(task->thread->completion,
qio_task_thread_result, task, NULL);
g_source_attach(task->thread->completion,
task->thread->context);
trace_qio_task_thread_source_attach(task, task->thread->completion);
qemu_cond_signal(&task->thread_cond);
qemu_mutex_unlock(&task->thread_lock);
return NULL;
}
@ -138,21 +158,38 @@ void qio_task_run_in_thread(QIOTask *task,
g_main_context_ref(context);
}
data->task = task;
data->worker = worker;
data->opaque = opaque;
data->destroy = destroy;
data->context = context;
task->thread = data;
trace_qio_task_thread_start(task, worker, opaque);
qemu_thread_create(&thread,
"io-task-worker",
qio_task_thread_worker,
data,
task,
QEMU_THREAD_DETACHED);
}
void qio_task_wait_thread(QIOTask *task)
{
qemu_mutex_lock(&task->thread_lock);
g_assert(task->thread != NULL);
while (task->thread->completion == NULL) {
qemu_cond_wait(&task->thread_cond, &task->thread_lock);
}
trace_qio_task_thread_source_cancel(task, task->thread->completion);
g_source_destroy(task->thread->completion);
qemu_mutex_unlock(&task->thread_lock);
qio_task_thread_result(task);
}
void qio_task_complete(QIOTask *task)
{
task->func(task, task->opaque);

View File

@ -7,6 +7,8 @@ qio_task_thread_start(void *task, void *worker, void *opaque) "Task thread start
qio_task_thread_run(void *task) "Task thread run task=%p"
qio_task_thread_exit(void *task) "Task thread exit task=%p"
qio_task_thread_result(void *task) "Task thread result task=%p"
qio_task_thread_source_attach(void *task, void *source) "Task thread source attach task=%p source=%p"
qio_task_thread_source_cancel(void *task, void *source) "Task thread source cancel task=%p source=%p"
# io/channel-socket.c
qio_channel_socket_new(void *ioc) "Socket new ioc=%p"

View File

@ -927,7 +927,7 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
* FIXME: sure we want to support implicit
* muxed monitors here?
*/
Chardev *chr = qemu_chr_new_mux_mon(buf, p);
Chardev *chr = qemu_chr_new_mux_mon(buf, p, NULL);
if (!chr) {
error_setg(errp, "Could not open guest forwarding device '%s'",

View File

@ -763,7 +763,7 @@ void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
{
Chardev *chr;
chr = qemu_chr_new("qtest", qtest_chrdev);
chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
if (chr == NULL) {
error_setg(errp, "Failed to initialize device for qtest: \"%s\"",

View File

@ -295,7 +295,7 @@ static void setup_vm_with_server(IVState *s, int nvectors)
{
char *cmd;
cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait "
cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s "
"-device ivshmem-doorbell,chardev=chr0,vectors=%d",
tmpserver, nvectors);

View File

@ -232,9 +232,9 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
qtest_add_abrt_handler(kill_qemu_hook_func, s);
command = g_strdup_printf("exec %s "
"-qtest unix:%s,nowait "
"-qtest unix:%s "
"-qtest-log %s "
"-chardev socket,path=%s,nowait,id=char0 "
"-chardev socket,path=%s,id=char0 "
"-mon chardev=char0,mode=control "
"-machine accel=qtest "
"-display none "

View File

@ -11,11 +11,17 @@
#include "qapi/qapi-commands-char.h"
#include "qapi/qmp/qdict.h"
#include "qom/qom-qobject.h"
#include "io/channel-socket.h"
#include "qapi/qobject-input-visitor.h"
#include "qapi/qapi-visit-sockets.h"
static bool quit;
typedef struct FeHandler {
int read_count;
bool is_open;
int openclose_count;
bool openclose_mismatch;
int last_event;
char read_buf[128];
} FeHandler;
@ -49,10 +55,24 @@ static void fe_read(void *opaque, const uint8_t *buf, int size)
static void fe_event(void *opaque, int event)
{
FeHandler *h = opaque;
bool new_open_state;
h->last_event = event;
if (event != CHR_EVENT_BREAK) {
switch (event) {
case CHR_EVENT_BREAK:
break;
case CHR_EVENT_OPENED:
case CHR_EVENT_CLOSED:
h->openclose_count++;
new_open_state = (event == CHR_EVENT_OPENED);
if (h->is_open == new_open_state) {
h->openclose_mismatch = true;
}
h->is_open = new_open_state;
/* no break */
default:
quit = true;
break;
}
}
@ -66,7 +86,7 @@ static void char_console_test_subprocess(void)
1, &error_abort);
qemu_opt_set(opts, "backend", "console", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_nonnull(chr);
qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7);
@ -88,7 +108,7 @@ static void char_stdio_test_subprocess(void)
CharBackend be;
int ret;
chr = qemu_chr_new("label", "stdio");
chr = qemu_chr_new("label", "stdio", NULL);
g_assert_nonnull(chr);
qemu_chr_fe_init(&be, chr, &error_abort);
@ -119,7 +139,7 @@ static void char_ringbuf_test(void)
qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
qemu_opt_set(opts, "size", "5", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_null(chr);
qemu_opts_del(opts);
@ -127,7 +147,7 @@ static void char_ringbuf_test(void)
1, &error_abort);
qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
qemu_opt_set(opts, "size", "2", &error_abort);
chr = qemu_chr_new_from_opts(opts, &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
g_assert_nonnull(chr);
qemu_opts_del(opts);
@ -150,7 +170,7 @@ static void char_ringbuf_test(void)
1, &error_abort);
qemu_opt_set(opts, "backend", "memory", &error_abort);
qemu_opt_set(opts, "size", "2", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_nonnull(chr);
object_unparent(OBJECT(chr));
qemu_opts_del(opts);
@ -161,7 +181,7 @@ static void char_mux_test(void)
QemuOpts *opts;
Chardev *chr, *base;
char *data;
FeHandler h1 = { 0, }, h2 = { 0, };
FeHandler h1 = { 0, false, 0, false, }, h2 = { 0, false, 0, false, };
CharBackend chr_be1, chr_be2;
opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
@ -169,7 +189,7 @@ static void char_mux_test(void)
qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
qemu_opt_set(opts, "size", "128", &error_abort);
qemu_opt_set(opts, "mux", "on", &error_abort);
chr = qemu_chr_new_from_opts(opts, &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
g_assert_nonnull(chr);
qemu_opts_del(opts);
@ -233,6 +253,65 @@ static void char_mux_test(void)
g_assert_cmpint(h1.last_event, ==, CHR_EVENT_BREAK);
g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
/* open/close state and corresponding events */
g_assert_true(qemu_chr_fe_backend_open(&chr_be1));
g_assert_true(qemu_chr_fe_backend_open(&chr_be2));
g_assert_true(h1.is_open);
g_assert_false(h1.openclose_mismatch);
g_assert_true(h2.is_open);
g_assert_false(h2.openclose_mismatch);
h1.openclose_count = h2.openclose_count = 0;
qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL,
NULL, NULL, false);
qemu_chr_fe_set_handlers(&chr_be2, NULL, NULL, NULL, NULL,
NULL, NULL, false);
g_assert_cmpint(h1.openclose_count, ==, 0);
g_assert_cmpint(h2.openclose_count, ==, 0);
h1.is_open = h2.is_open = false;
qemu_chr_fe_set_handlers(&chr_be1,
NULL,
NULL,
fe_event,
NULL,
&h1,
NULL, false);
qemu_chr_fe_set_handlers(&chr_be2,
NULL,
NULL,
fe_event,
NULL,
&h2,
NULL, false);
g_assert_cmpint(h1.openclose_count, ==, 1);
g_assert_false(h1.openclose_mismatch);
g_assert_cmpint(h2.openclose_count, ==, 1);
g_assert_false(h2.openclose_mismatch);
qemu_chr_be_event(base, CHR_EVENT_CLOSED);
qemu_chr_be_event(base, CHR_EVENT_OPENED);
g_assert_cmpint(h1.openclose_count, ==, 3);
g_assert_false(h1.openclose_mismatch);
g_assert_cmpint(h2.openclose_count, ==, 3);
g_assert_false(h2.openclose_mismatch);
qemu_chr_fe_set_handlers(&chr_be2,
fe_can_read,
fe_read,
fe_event,
NULL,
&h2,
NULL, false);
qemu_chr_fe_set_handlers(&chr_be1,
fe_can_read,
fe_read,
fe_event,
NULL,
&h1,
NULL, false);
/* remove first handler */
qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL,
NULL, NULL, true);
@ -257,168 +336,6 @@ static void char_mux_test(void)
qemu_chr_fe_deinit(&chr_be2, true);
}
typedef struct SocketIdleData {
GMainLoop *loop;
Chardev *chr;
bool conn_expected;
CharBackend *be;
CharBackend *client_be;
} SocketIdleData;
static gboolean char_socket_test_idle(gpointer user_data)
{
SocketIdleData *data = user_data;
if (object_property_get_bool(OBJECT(data->chr), "connected", NULL)
== data->conn_expected) {
quit = true;
return FALSE;
}
return TRUE;
}
static void socket_read(void *opaque, const uint8_t *buf, int size)
{
SocketIdleData *data = opaque;
g_assert_cmpint(size, ==, 1);
g_assert_cmpint(*buf, ==, 'Z');
size = qemu_chr_fe_write(data->be, (const uint8_t *)"hello", 5);
g_assert_cmpint(size, ==, 5);
}
static int socket_can_read(void *opaque)
{
return 10;
}
static void socket_read_hello(void *opaque, const uint8_t *buf, int size)
{
g_assert_cmpint(size, ==, 5);
g_assert(strncmp((char *)buf, "hello", 5) == 0);
quit = true;
}
static int socket_can_read_hello(void *opaque)
{
return 10;
}
static void char_socket_test_common(Chardev *chr, bool reconnect)
{
Chardev *chr_client;
QObject *addr;
QDict *qdict;
const char *port;
SocketIdleData d = { .chr = chr };
CharBackend be;
CharBackend client_be;
char *tmp;
d.be = &be;
d.client_be = &be;
g_assert_nonnull(chr);
g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
addr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
qdict = qobject_to(QDict, addr);
port = qdict_get_str(qdict, "port");
tmp = g_strdup_printf("tcp:127.0.0.1:%s%s", port,
reconnect ? ",reconnect=1" : "");
qobject_unref(qdict);
qemu_chr_fe_init(&be, chr, &error_abort);
qemu_chr_fe_set_handlers(&be, socket_can_read, socket_read,
NULL, NULL, &d, NULL, true);
chr_client = qemu_chr_new("client", tmp);
qemu_chr_fe_init(&client_be, chr_client, &error_abort);
qemu_chr_fe_set_handlers(&client_be, socket_can_read_hello,
socket_read_hello,
NULL, NULL, &d, NULL, true);
g_free(tmp);
d.conn_expected = true;
guint id = g_idle_add(char_socket_test_idle, &d);
g_source_set_name_by_id(id, "test-idle");
g_assert_cmpint(id, >, 0);
main_loop();
d.chr = chr_client;
id = g_idle_add(char_socket_test_idle, &d);
g_source_set_name_by_id(id, "test-idle");
g_assert_cmpint(id, >, 0);
main_loop();
g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
g_assert(object_property_get_bool(OBJECT(chr_client),
"connected", &error_abort));
qemu_chr_write_all(chr_client, (const uint8_t *)"Z", 1);
main_loop();
object_unparent(OBJECT(chr_client));
d.chr = chr;
d.conn_expected = false;
g_idle_add(char_socket_test_idle, &d);
main_loop();
object_unparent(OBJECT(chr));
}
static void char_socket_basic_test(void)
{
Chardev *chr = qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
char_socket_test_common(chr, false);
}
static void char_socket_reconnect_test(void)
{
Chardev *chr = qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
char_socket_test_common(chr, true);
}
static void char_socket_fdpass_test(void)
{
Chardev *chr;
char *optstr;
QemuOpts *opts;
int fd;
SocketAddress *addr = g_new0(SocketAddress, 1);
addr->type = SOCKET_ADDRESS_TYPE_INET;
addr->u.inet.host = g_strdup("127.0.0.1");
addr->u.inet.port = g_strdup("0");
fd = socket_listen(addr, &error_abort);
g_assert(fd >= 0);
qapi_free_SocketAddress(addr);
optstr = g_strdup_printf("socket,id=cdev,fd=%d,server,nowait", fd);
opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
optstr, true);
g_free(optstr);
g_assert_nonnull(opts);
chr = qemu_chr_new_from_opts(opts, &error_abort);
qemu_opts_del(opts);
char_socket_test_common(chr, false);
}
static void websock_server_read(void *opaque, const uint8_t *buf, int size)
{
@ -495,7 +412,7 @@ static void char_websock_test(void)
CharBackend client_be;
Chardev *chr_client;
Chardev *chr = qemu_chr_new("server",
"websocket:127.0.0.1:0,server,nowait");
"websocket:127.0.0.1:0,server,nowait", NULL);
const char handshake[] = "GET / HTTP/1.1\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
@ -519,7 +436,7 @@ static void char_websock_test(void)
qemu_chr_fe_set_handlers(&be, websock_server_can_read, websock_server_read,
NULL, NULL, chr, NULL, true);
chr_client = qemu_chr_new("client", tmp);
chr_client = qemu_chr_new("client", tmp, NULL);
qemu_chr_fe_init(&client_be, chr_client, &error_abort);
qemu_chr_fe_set_handlers(&client_be, websock_client_can_read,
websock_client_read,
@ -565,7 +482,7 @@ static void char_pipe_test(void)
}
tmp = g_strdup_printf("pipe:%s", pipe);
chr = qemu_chr_new("pipe", tmp);
chr = qemu_chr_new("pipe", tmp, NULL);
g_assert_nonnull(chr);
g_free(tmp);
@ -610,6 +527,28 @@ static void char_pipe_test(void)
}
#endif
typedef struct SocketIdleData {
GMainLoop *loop;
Chardev *chr;
bool conn_expected;
CharBackend *be;
CharBackend *client_be;
} SocketIdleData;
static void socket_read_hello(void *opaque, const uint8_t *buf, int size)
{
g_assert_cmpint(size, ==, 5);
g_assert(strncmp((char *)buf, "hello", 5) == 0);
quit = true;
}
static int socket_can_read_hello(void *opaque)
{
return 10;
}
static int make_udp_socket(int *port)
{
struct sockaddr_in addr = { 0, };
@ -647,7 +586,7 @@ static void char_udp_test_internal(Chardev *reuse_chr, int sock)
int port;
sock = make_udp_socket(&port);
tmp = g_strdup_printf("udp:127.0.0.1:%d", port);
chr = qemu_chr_new("client", tmp);
chr = qemu_chr_new("client", tmp, NULL);
g_assert_nonnull(chr);
be = g_alloca(sizeof(CharBackend));
@ -680,6 +619,391 @@ static void char_udp_test(void)
char_udp_test_internal(NULL, 0);
}
typedef struct {
int event;
bool got_pong;
} CharSocketTestData;
#define SOCKET_PING "Hello"
#define SOCKET_PONG "World"
static void
char_socket_event(void *opaque, int event)
{
CharSocketTestData *data = opaque;
data->event = event;
}
static void
char_socket_read(void *opaque, const uint8_t *buf, int size)
{
CharSocketTestData *data = opaque;
g_assert_cmpint(size, ==, sizeof(SOCKET_PONG));
g_assert(memcmp(buf, SOCKET_PONG, size) == 0);
data->got_pong = true;
}
static int
char_socket_can_read(void *opaque)
{
return sizeof(SOCKET_PONG);
}
static char *
char_socket_addr_to_opt_str(SocketAddress *addr, bool fd_pass,
const char *reconnect, bool is_listen)
{
if (fd_pass) {
QIOChannelSocket *ioc = qio_channel_socket_new();
int fd;
char *optstr;
g_assert(!reconnect);
if (is_listen) {
qio_channel_socket_listen_sync(ioc, addr, &error_abort);
} else {
qio_channel_socket_connect_sync(ioc, addr, &error_abort);
}
fd = ioc->fd;
ioc->fd = -1;
optstr = g_strdup_printf("socket,id=cdev0,fd=%d%s",
fd, is_listen ? ",server,nowait" : "");
object_unref(OBJECT(ioc));
return optstr;
} else {
switch (addr->type) {
case SOCKET_ADDRESS_TYPE_INET:
return g_strdup_printf("socket,id=cdev0,host=%s,port=%s%s%s",
addr->u.inet.host,
addr->u.inet.port,
reconnect ? reconnect : "",
is_listen ? ",server,nowait" : "");
case SOCKET_ADDRESS_TYPE_UNIX:
return g_strdup_printf("socket,id=cdev0,path=%s%s%s",
addr->u.q_unix.path,
reconnect ? reconnect : "",
is_listen ? ",server,nowait" : "");
default:
g_assert_not_reached();
}
}
}
static void
char_socket_ping_pong(QIOChannel *ioc)
{
char greeting[sizeof(SOCKET_PING)];
const char *response = SOCKET_PONG;
qio_channel_read_all(ioc, greeting, sizeof(greeting), &error_abort);
g_assert(memcmp(greeting, SOCKET_PING, sizeof(greeting)) == 0);
qio_channel_write_all(ioc, response, sizeof(SOCKET_PONG), &error_abort);
object_unref(OBJECT(ioc));
}
static gpointer
char_socket_server_client_thread(gpointer data)
{
SocketAddress *addr = data;
QIOChannelSocket *ioc = qio_channel_socket_new();
qio_channel_socket_connect_sync(ioc, addr, &error_abort);
char_socket_ping_pong(QIO_CHANNEL(ioc));
return NULL;
}
typedef struct {
SocketAddress *addr;
bool wait_connected;
bool fd_pass;
} CharSocketServerTestConfig;
static void char_socket_server_test(gconstpointer opaque)
{
const CharSocketServerTestConfig *config = opaque;
Chardev *chr;
CharBackend be = {0};
CharSocketTestData data = {0};
QObject *qaddr;
SocketAddress *addr;
Visitor *v;
QemuThread thread;
int ret;
bool reconnected;
char *optstr;
QemuOpts *opts;
g_setenv("QTEST_SILENT_ERRORS", "1", 1);
/*
* We rely on config->addr containing "nowait", otherwise
* qemu_chr_new() will block until a client connects. We
* can't spawn our client thread though, because until
* qemu_chr_new() returns we don't know what TCP port was
* allocated by the OS
*/
optstr = char_socket_addr_to_opt_str(config->addr,
config->fd_pass,
NULL,
true);
opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
optstr, true);
g_assert_nonnull(opts);
chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
qemu_opts_del(opts);
g_assert_nonnull(chr);
g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
g_assert_nonnull(qaddr);
v = qobject_input_visitor_new(qaddr);
visit_type_SocketAddress(v, "addr", &addr, &error_abort);
visit_free(v);
qobject_unref(qaddr);
qemu_chr_fe_init(&be, chr, &error_abort);
reconnect:
data.event = -1;
qemu_chr_fe_set_handlers(&be, NULL, NULL,
char_socket_event, NULL,
&data, NULL, true);
g_assert(data.event == -1);
/*
* Kick off a thread to act as the "remote" client
* which just plays ping-pong with us
*/
qemu_thread_create(&thread, "client",
char_socket_server_client_thread,
addr, QEMU_THREAD_JOINABLE);
g_assert(data.event == -1);
if (config->wait_connected) {
/* Synchronously accept a connection */
qemu_chr_wait_connected(chr, &error_abort);
} else {
/*
* Asynchronously accept a connection when the evnt
* loop reports the listener socket as readable
*/
while (data.event == -1) {
main_loop_wait(false);
}
}
g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
g_assert(data.event == CHR_EVENT_OPENED);
data.event = -1;
/* Send a greeting to the client */
ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING,
sizeof(SOCKET_PING));
g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
g_assert(data.event == -1);
/* Setup a callback to receive the reply to our greeting */
qemu_chr_fe_set_handlers(&be, char_socket_can_read,
char_socket_read,
char_socket_event, NULL,
&data, NULL, true);
g_assert(data.event == CHR_EVENT_OPENED);
data.event = -1;
/* Wait for the client to go away */
while (data.event == -1) {
main_loop_wait(false);
}
g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
g_assert(data.event == CHR_EVENT_CLOSED);
g_assert(data.got_pong);
qemu_thread_join(&thread);
if (!reconnected) {
reconnected = true;
goto reconnect;
}
qapi_free_SocketAddress(addr);
object_unparent(OBJECT(chr));
g_free(optstr);
g_unsetenv("QTEST_SILENT_ERRORS");
}
static gpointer
char_socket_client_server_thread(gpointer data)
{
QIOChannelSocket *ioc = data;
QIOChannelSocket *cioc;
cioc = qio_channel_socket_accept(ioc, &error_abort);
g_assert_nonnull(cioc);
char_socket_ping_pong(QIO_CHANNEL(cioc));
return NULL;
}
typedef struct {
SocketAddress *addr;
const char *reconnect;
bool wait_connected;
bool fd_pass;
} CharSocketClientTestConfig;
static void char_socket_client_test(gconstpointer opaque)
{
const CharSocketClientTestConfig *config = opaque;
QIOChannelSocket *ioc;
char *optstr;
Chardev *chr;
CharBackend be = {0};
CharSocketTestData data = {0};
SocketAddress *addr;
QemuThread thread;
int ret;
bool reconnected = false;
QemuOpts *opts;
/*
* Setup a listener socket and determine get its address
* so we know the TCP port for the client later
*/
ioc = qio_channel_socket_new();
g_assert_nonnull(ioc);
qio_channel_socket_listen_sync(ioc, config->addr, &error_abort);
addr = qio_channel_socket_get_local_address(ioc, &error_abort);
g_assert_nonnull(addr);
/*
* Kick off a thread to act as the "remote" client
* which just plays ping-pong with us
*/
qemu_thread_create(&thread, "client",
char_socket_client_server_thread,
ioc, QEMU_THREAD_JOINABLE);
/*
* Populate the chardev address based on what the server
* is actually listening on
*/
optstr = char_socket_addr_to_opt_str(addr,
config->fd_pass,
config->reconnect,
false);
opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
optstr, true);
g_assert_nonnull(opts);
chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
qemu_opts_del(opts);
g_assert_nonnull(chr);
if (config->reconnect) {
/*
* If reconnect is set, the connection will be
* established in a background thread and we won't
* see the "connected" status updated until we
* run the main event loop, or call qemu_chr_wait_connected
*/
g_assert(!object_property_get_bool(OBJECT(chr), "connected",
&error_abort));
} else {
g_assert(object_property_get_bool(OBJECT(chr), "connected",
&error_abort));
}
qemu_chr_fe_init(&be, chr, &error_abort);
reconnect:
data.event = -1;
qemu_chr_fe_set_handlers(&be, NULL, NULL,
char_socket_event, NULL,
&data, NULL, true);
if (config->reconnect) {
g_assert(data.event == -1);
} else {
g_assert(data.event == CHR_EVENT_OPENED);
}
if (config->wait_connected) {
/*
* Synchronously wait for the connection to complete
* This should be a no-op if reconnect is not set.
*/
qemu_chr_wait_connected(chr, &error_abort);
} else {
/*
* Asynchronously wait for the connection to be reported
* as complete when the background thread reports its
* status.
* The loop will short-circuit if reconnect was set
*/
while (data.event == -1) {
main_loop_wait(false);
}
}
g_assert(data.event == CHR_EVENT_OPENED);
data.event = -1;
g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
/* Send a greeting to the server */
ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING,
sizeof(SOCKET_PING));
g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
g_assert(data.event == -1);
/* Setup a callback to receive the reply to our greeting */
qemu_chr_fe_set_handlers(&be, char_socket_can_read,
char_socket_read,
char_socket_event, NULL,
&data, NULL, true);
g_assert(data.event == CHR_EVENT_OPENED);
data.event = -1;
/* Wait for the server to go away */
while (data.event == -1) {
main_loop_wait(false);
}
g_assert(data.event == CHR_EVENT_CLOSED);
g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
g_assert(data.got_pong);
qemu_thread_join(&thread);
if (config->reconnect && !reconnected) {
reconnected = true;
qemu_thread_create(&thread, "client",
char_socket_client_server_thread,
ioc, QEMU_THREAD_JOINABLE);
goto reconnect;
}
object_unref(OBJECT(ioc));
object_unparent(OBJECT(chr));
qapi_free_SocketAddress(addr);
g_free(optstr);
}
#ifdef HAVE_CHARDEV_SERIAL
static void char_serial_test(void)
{
@ -691,14 +1015,14 @@ static void char_serial_test(void)
qemu_opt_set(opts, "backend", "serial", &error_abort);
qemu_opt_set(opts, "path", "/dev/null", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_nonnull(chr);
/* TODO: add more tests with a pty */
object_unparent(OBJECT(chr));
/* test tty alias */
qemu_opt_set(opts, "backend", "tty", &error_abort);
chr = qemu_chr_new_from_opts(opts, NULL);
chr = qemu_chr_new_from_opts(opts, NULL, NULL);
g_assert_nonnull(chr);
object_unparent(OBJECT(chr));
@ -731,7 +1055,7 @@ static void char_file_fifo_test(void)
g_assert_cmpint(ret, ==, 8);
chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend,
&error_abort);
NULL, &error_abort);
qemu_chr_fe_init(&be, chr, &error_abort);
qemu_chr_fe_set_handlers(&be,
@ -785,7 +1109,7 @@ static void char_file_test_internal(Chardev *ext_chr, const char *filepath)
out = g_build_filename(tmp_path, "out", NULL);
file.out = out;
chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
&error_abort);
NULL, &error_abort);
}
ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6);
g_assert_cmpint(ret, ==, 6);
@ -820,7 +1144,7 @@ static void char_null_test(void)
chr = qemu_chr_find("label-null");
g_assert_null(chr);
chr = qemu_chr_new("label-null", "null");
chr = qemu_chr_new("label-null", "null", NULL);
chr = qemu_chr_find("label-null");
g_assert_nonnull(chr);
@ -856,9 +1180,10 @@ static void char_null_test(void)
static void char_invalid_test(void)
{
Chardev *chr;
chr = qemu_chr_new("label-invalid", "invalid");
g_setenv("QTEST_SILENT_ERRORS", "1", 1);
chr = qemu_chr_new("label-invalid", "invalid", NULL);
g_assert_null(chr);
g_unsetenv("QTEST_SILENT_ERRORS");
}
static int chardev_change(void *opaque)
@ -890,7 +1215,7 @@ static void char_hotswap_test(void)
chr_args = g_strdup_printf("udp:127.0.0.1:%d", port);
chr = qemu_chr_new("chardev", chr_args);
chr = qemu_chr_new("chardev", chr_args, NULL);
qemu_chr_fe_init(&be, chr, &error_abort);
/* check that chardev operates correctly */
@ -958,9 +1283,71 @@ int main(int argc, char **argv)
#ifndef _WIN32
g_test_add_func("/char/file-fifo", char_file_fifo_test);
#endif
g_test_add_func("/char/socket/basic", char_socket_basic_test);
g_test_add_func("/char/socket/reconnect", char_socket_reconnect_test);
g_test_add_func("/char/socket/fdpass", char_socket_fdpass_test);
SocketAddress tcpaddr = {
.type = SOCKET_ADDRESS_TYPE_INET,
.u.inet.host = (char *)"127.0.0.1",
.u.inet.port = (char *)"0",
};
#ifndef WIN32
SocketAddress unixaddr = {
.type = SOCKET_ADDRESS_TYPE_UNIX,
.u.q_unix.path = (char *)"test-char.sock",
};
#endif
#define SOCKET_SERVER_TEST(name, addr) \
CharSocketServerTestConfig server1 ## name = \
{ addr, false, false }; \
CharSocketServerTestConfig server2 ## name = \
{ addr, true, false }; \
CharSocketServerTestConfig server3 ## name = \
{ addr, false, true }; \
CharSocketServerTestConfig server4 ## name = \
{ addr, true, true }; \
g_test_add_data_func("/char/socket/server/mainloop/" # name, \
&server1 ##name, char_socket_server_test); \
g_test_add_data_func("/char/socket/server/wait-conn/" # name, \
&server2 ##name, char_socket_server_test); \
g_test_add_data_func("/char/socket/server/mainloop-fdpass/" # name, \
&server3 ##name, char_socket_server_test); \
g_test_add_data_func("/char/socket/server/wait-conn-fdpass/" # name, \
&server4 ##name, char_socket_server_test)
#define SOCKET_CLIENT_TEST(name, addr) \
CharSocketClientTestConfig client1 ## name = \
{ addr, NULL, false, false }; \
CharSocketClientTestConfig client2 ## name = \
{ addr, NULL, true, false }; \
CharSocketClientTestConfig client3 ## name = \
{ addr, ",reconnect=1", false }; \
CharSocketClientTestConfig client4 ## name = \
{ addr, ",reconnect=1", true }; \
CharSocketClientTestConfig client5 ## name = \
{ addr, NULL, false, true }; \
CharSocketClientTestConfig client6 ## name = \
{ addr, NULL, true, true }; \
g_test_add_data_func("/char/socket/client/mainloop/" # name, \
&client1 ##name, char_socket_client_test); \
g_test_add_data_func("/char/socket/client/wait-conn/" # name, \
&client2 ##name, char_socket_client_test); \
g_test_add_data_func("/char/socket/client/mainloop-reconnect/" # name, \
&client3 ##name, char_socket_client_test); \
g_test_add_data_func("/char/socket/client/wait-conn-reconnect/" # name, \
&client4 ##name, char_socket_client_test); \
g_test_add_data_func("/char/socket/client/mainloop-fdpass/" # name, \
&client5 ##name, char_socket_client_test); \
g_test_add_data_func("/char/socket/client/wait-conn-fdpass/" # name, \
&client6 ##name, char_socket_client_test)
SOCKET_SERVER_TEST(tcp, &tcpaddr);
SOCKET_CLIENT_TEST(tcp, &tcpaddr);
#ifndef WIN32
SOCKET_SERVER_TEST(unix, &unixaddr);
SOCKET_CLIENT_TEST(unix, &unixaddr);
#endif
g_test_add_func("/char/udp", char_udp_test);
#ifdef HAVE_CHARDEV_SERIAL
g_test_add_func("/char/serial", char_serial_test);

View File

@ -96,7 +96,7 @@ static void test_redirector_tx(void)
"-device %s,netdev=qtest-bn0,id=qtest-e0 "
"-chardev socket,id=redirector0,path=%s,server,nowait "
"-chardev socket,id=redirector1,path=%s,server,nowait "
"-chardev socket,id=redirector2,path=%s,nowait "
"-chardev socket,id=redirector2,path=%s "
"-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
"queue=tx,outdev=redirector0 "
"-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"
@ -166,7 +166,7 @@ static void test_redirector_rx(void)
"-device %s,netdev=qtest-bn0,id=qtest-e0 "
"-chardev socket,id=redirector0,path=%s,server,nowait "
"-chardev socket,id=redirector1,path=%s,server,nowait "
"-chardev socket,id=redirector2,path=%s,nowait "
"-chardev socket,id=redirector2,path=%s "
"-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
"queue=rx,indev=redirector0 "
"-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"

View File

@ -519,7 +519,7 @@ static void test_server_create_chr(TestServer *server, const gchar *opt)
Chardev *chr;
chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
chr = qemu_chr_new(server->chr_name, chr_path);
chr = qemu_chr_new(server->chr_name, chr_path, NULL);
g_free(chr_path);
g_assert_nonnull(chr);

8
vl.c
View File

@ -2302,7 +2302,7 @@ static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
{
Error *local_err = NULL;
if (!qemu_chr_new_from_opts(opts, &local_err)) {
if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
if (local_err) {
error_propagate(errp, local_err);
return -1;
@ -2440,7 +2440,7 @@ static int serial_parse(const char *devname)
snprintf(label, sizeof(label), "serial%d", index);
serial_hds = g_renew(Chardev *, serial_hds, index + 1);
serial_hds[index] = qemu_chr_new_mux_mon(label, devname);
serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
if (!serial_hds[index]) {
error_report("could not connect serial device"
" to character backend '%s'", devname);
@ -2476,7 +2476,7 @@ static int parallel_parse(const char *devname)
exit(1);
}
snprintf(label, sizeof(label), "parallel%d", index);
parallel_hds[index] = qemu_chr_new_mux_mon(label, devname);
parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
if (!parallel_hds[index]) {
error_report("could not connect parallel device"
" to character backend '%s'", devname);
@ -2490,7 +2490,7 @@ static int debugcon_parse(const char *devname)
{
QemuOpts *opts;
if (!qemu_chr_new_mux_mon("debugcon", devname)) {
if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
error_report("invalid character backend '%s'", devname);
exit(1);
}