summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/colo-compare.c59
-rw-r--r--net/filter-mirror.c64
-rw-r--r--net/slirp.c25
-rw-r--r--net/vhost-user.c49
4 files changed, 99 insertions, 98 deletions
diff --git a/net/colo-compare.c b/net/colo-compare.c
index 47703c59bc..109990fd8e 100644
--- a/net/colo-compare.c
+++ b/net/colo-compare.c
@@ -68,9 +68,9 @@ typedef struct CompareState {
char *pri_indev;
char *sec_indev;
char *outdev;
- CharDriverState *chr_pri_in;
- CharDriverState *chr_sec_in;
- CharDriverState *chr_out;
+ CharBackend chr_pri_in;
+ CharBackend chr_sec_in;
+ CharBackend chr_out;
SocketReadState pri_rs;
SocketReadState sec_rs;
@@ -101,7 +101,7 @@ enum {
SECONDARY_IN,
};
-static int compare_chr_send(CharDriverState *out,
+static int compare_chr_send(CharBackend *out,
const uint8_t *buf,
uint32_t size);
@@ -385,7 +385,7 @@ static void colo_compare_connection(void *opaque, void *user_data)
}
if (result) {
- ret = compare_chr_send(s->chr_out, pkt->data, pkt->size);
+ ret = compare_chr_send(&s->chr_out, pkt->data, pkt->size);
if (ret < 0) {
error_report("colo_send_primary_packet failed");
}
@@ -408,7 +408,7 @@ static void colo_compare_connection(void *opaque, void *user_data)
}
}
-static int compare_chr_send(CharDriverState *out,
+static int compare_chr_send(CharBackend *out,
const uint8_t *buf,
uint32_t size)
{
@@ -451,7 +451,8 @@ static void compare_pri_chr_in(void *opaque, const uint8_t *buf, int size)
ret = net_fill_rstate(&s->pri_rs, buf, size);
if (ret == -1) {
- qemu_chr_add_handlers(s->chr_pri_in, NULL, NULL, NULL, NULL);
+ qemu_chr_fe_set_handlers(&s->chr_pri_in, NULL, NULL, NULL,
+ NULL, NULL, true);
error_report("colo-compare primary_in error");
}
}
@@ -467,7 +468,8 @@ static void compare_sec_chr_in(void *opaque, const uint8_t *buf, int size)
ret = net_fill_rstate(&s->sec_rs, buf, size);
if (ret == -1) {
- qemu_chr_add_handlers(s->chr_sec_in, NULL, NULL, NULL, NULL);
+ qemu_chr_fe_set_handlers(&s->chr_sec_in, NULL, NULL, NULL,
+ NULL, NULL, true);
error_report("colo-compare secondary_in error");
}
}
@@ -480,10 +482,10 @@ static void *colo_compare_thread(void *opaque)
worker_context = g_main_context_new();
- qemu_chr_add_handlers_full(s->chr_pri_in, compare_chr_can_read,
- compare_pri_chr_in, NULL, s, worker_context);
- qemu_chr_add_handlers_full(s->chr_sec_in, compare_chr_can_read,
- compare_sec_chr_in, NULL, s, worker_context);
+ qemu_chr_fe_set_handlers(&s->chr_pri_in, compare_chr_can_read,
+ compare_pri_chr_in, NULL, s, worker_context, true);
+ qemu_chr_fe_set_handlers(&s->chr_sec_in, compare_chr_can_read,
+ compare_sec_chr_in, NULL, s, worker_context, true);
compare_loop = g_main_loop_new(worker_context, FALSE);
@@ -545,7 +547,7 @@ static void compare_pri_rs_finalize(SocketReadState *pri_rs)
if (packet_enqueue(s, PRIMARY_IN)) {
trace_colo_compare_main("primary: unsupported packet in");
- compare_chr_send(s->chr_out, pri_rs->buf, pri_rs->packet_len);
+ compare_chr_send(&s->chr_out, pri_rs->buf, pri_rs->packet_len);
} else {
/* compare connection */
g_queue_foreach(&s->conn_list, colo_compare_connection, s);
@@ -589,6 +591,7 @@ static int find_and_check_chardev(CharDriverState **chr,
chr_name);
return 1;
}
+
return 0;
}
@@ -619,6 +622,7 @@ static void check_old_packet_regular(void *opaque)
static void colo_compare_complete(UserCreatable *uc, Error **errp)
{
CompareState *s = COLO_COMPARE(uc);
+ CharDriverState *chr;
char thread_name[64];
static int compare_id;
@@ -634,24 +638,21 @@ static void colo_compare_complete(UserCreatable *uc, Error **errp)
return;
}
- if (find_and_check_chardev(&s->chr_pri_in, s->pri_indev, errp)) {
+ if (find_and_check_chardev(&chr, s->pri_indev, errp) ||
+ !qemu_chr_fe_init(&s->chr_pri_in, chr, errp)) {
return;
}
- if (find_and_check_chardev(&s->chr_sec_in, s->sec_indev, errp)) {
+ if (find_and_check_chardev(&chr, s->sec_indev, errp) ||
+ !qemu_chr_fe_init(&s->chr_sec_in, chr, errp)) {
return;
}
- if (find_and_check_chardev(&s->chr_out, s->outdev, errp)) {
+ if (find_and_check_chardev(&chr, s->outdev, errp) ||
+ !qemu_chr_fe_init(&s->chr_out, chr, errp)) {
return;
}
- qemu_chr_fe_claim_no_fail(s->chr_pri_in);
-
- qemu_chr_fe_claim_no_fail(s->chr_sec_in);
-
- qemu_chr_fe_claim_no_fail(s->chr_out);
-
net_socket_rs_init(&s->pri_rs, compare_pri_rs_finalize);
net_socket_rs_init(&s->sec_rs, compare_sec_rs_finalize);
@@ -702,17 +703,9 @@ static void colo_compare_finalize(Object *obj)
{
CompareState *s = COLO_COMPARE(obj);
- if (s->chr_pri_in) {
- qemu_chr_add_handlers(s->chr_pri_in, NULL, NULL, NULL, NULL);
- qemu_chr_fe_release(s->chr_pri_in);
- }
- if (s->chr_sec_in) {
- qemu_chr_add_handlers(s->chr_sec_in, NULL, NULL, NULL, NULL);
- qemu_chr_fe_release(s->chr_sec_in);
- }
- if (s->chr_out) {
- qemu_chr_fe_release(s->chr_out);
- }
+ qemu_chr_fe_deinit(&s->chr_pri_in);
+ qemu_chr_fe_deinit(&s->chr_sec_in);
+ qemu_chr_fe_deinit(&s->chr_out);
g_queue_free(&s->conn_list);
diff --git a/net/filter-mirror.c b/net/filter-mirror.c
index 0ee58d905e..b7d645617c 100644
--- a/net/filter-mirror.c
+++ b/net/filter-mirror.c
@@ -38,12 +38,12 @@ typedef struct MirrorState {
NetFilterState parent_obj;
char *indev;
char *outdev;
- CharDriverState *chr_in;
- CharDriverState *chr_out;
+ CharBackend chr_in;
+ CharBackend chr_out;
SocketReadState rs;
} MirrorState;
-static int filter_mirror_send(CharDriverState *chr_out,
+static int filter_mirror_send(CharBackend *chr_out,
const struct iovec *iov,
int iovcnt)
{
@@ -110,7 +110,8 @@ static void redirector_chr_read(void *opaque, const uint8_t *buf, int size)
ret = net_fill_rstate(&s->rs, buf, size);
if (ret == -1) {
- qemu_chr_add_handlers(s->chr_in, NULL, NULL, NULL, NULL);
+ qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
+ NULL, NULL, true);
}
}
@@ -121,7 +122,8 @@ static void redirector_chr_event(void *opaque, int event)
switch (event) {
case CHR_EVENT_CLOSED:
- qemu_chr_add_handlers(s->chr_in, NULL, NULL, NULL, NULL);
+ qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
+ NULL, NULL, true);
break;
default:
break;
@@ -138,7 +140,7 @@ static ssize_t filter_mirror_receive_iov(NetFilterState *nf,
MirrorState *s = FILTER_MIRROR(nf);
int ret;
- ret = filter_mirror_send(s->chr_out, iov, iovcnt);
+ ret = filter_mirror_send(&s->chr_out, iov, iovcnt);
if (ret) {
error_report("filter_mirror_send failed(%s)", strerror(-ret));
}
@@ -160,8 +162,8 @@ static ssize_t filter_redirector_receive_iov(NetFilterState *nf,
MirrorState *s = FILTER_REDIRECTOR(nf);
int ret;
- if (s->chr_out) {
- ret = filter_mirror_send(s->chr_out, iov, iovcnt);
+ if (qemu_chr_fe_get_driver(&s->chr_out)) {
+ ret = filter_mirror_send(&s->chr_out, iov, iovcnt);
if (ret) {
error_report("filter_mirror_send failed(%s)", strerror(-ret));
}
@@ -175,27 +177,21 @@ static void filter_mirror_cleanup(NetFilterState *nf)
{
MirrorState *s = FILTER_MIRROR(nf);
- if (s->chr_out) {
- qemu_chr_fe_release(s->chr_out);
- }
+ qemu_chr_fe_deinit(&s->chr_out);
}
static void filter_redirector_cleanup(NetFilterState *nf)
{
MirrorState *s = FILTER_REDIRECTOR(nf);
- if (s->chr_in) {
- qemu_chr_add_handlers(s->chr_in, NULL, NULL, NULL, NULL);
- qemu_chr_fe_release(s->chr_in);
- }
- if (s->chr_out) {
- qemu_chr_fe_release(s->chr_out);
- }
+ qemu_chr_fe_deinit(&s->chr_in);
+ qemu_chr_fe_deinit(&s->chr_out);
}
static void filter_mirror_setup(NetFilterState *nf, Error **errp)
{
MirrorState *s = FILTER_MIRROR(nf);
+ CharDriverState *chr;
if (!s->outdev) {
error_setg(errp, "filter mirror needs 'outdev' "
@@ -203,17 +199,14 @@ static void filter_mirror_setup(NetFilterState *nf, Error **errp)
return;
}
- s->chr_out = qemu_chr_find(s->outdev);
- if (s->chr_out == NULL) {
+ chr = qemu_chr_find(s->outdev);
+ if (chr == NULL) {
error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
"Device '%s' not found", s->outdev);
return;
}
- if (qemu_chr_fe_claim(s->chr_out) != 0) {
- error_setg(errp, QERR_DEVICE_IN_USE, s->outdev);
- return;
- }
+ qemu_chr_fe_init(&s->chr_out, chr, errp);
}
static void redirector_rs_finalize(SocketReadState *rs)
@@ -227,6 +220,7 @@ static void redirector_rs_finalize(SocketReadState *rs)
static void filter_redirector_setup(NetFilterState *nf, Error **errp)
{
MirrorState *s = FILTER_REDIRECTOR(nf);
+ CharDriverState *chr;
if (!s->indev && !s->outdev) {
error_setg(errp, "filter redirector needs 'indev' or "
@@ -243,26 +237,32 @@ static void filter_redirector_setup(NetFilterState *nf, Error **errp)
net_socket_rs_init(&s->rs, redirector_rs_finalize);
if (s->indev) {
- s->chr_in = qemu_chr_find(s->indev);
- if (s->chr_in == NULL) {
+ chr = qemu_chr_find(s->indev);
+ if (chr == NULL) {
error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
"IN Device '%s' not found", s->indev);
return;
}
- qemu_chr_fe_claim_no_fail(s->chr_in);
- qemu_chr_add_handlers(s->chr_in, redirector_chr_can_read,
- redirector_chr_read, redirector_chr_event, nf);
+ if (!qemu_chr_fe_init(&s->chr_in, chr, errp)) {
+ return;
+ }
+
+ qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
+ redirector_chr_read, redirector_chr_event,
+ nf, NULL, true);
}
if (s->outdev) {
- s->chr_out = qemu_chr_find(s->outdev);
- if (s->chr_out == NULL) {
+ chr = qemu_chr_find(s->outdev);
+ if (chr == NULL) {
error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
"OUT Device '%s' not found", s->outdev);
return;
}
- qemu_chr_fe_claim_no_fail(s->chr_out);
+ if (!qemu_chr_fe_init(&s->chr_out, chr, errp)) {
+ return;
+ }
}
}
diff --git a/net/slirp.c b/net/slirp.c
index b60893f9c5..64dd3255ae 100644
--- a/net/slirp.c
+++ b/net/slirp.c
@@ -40,6 +40,7 @@
#include "sysemu/char.h"
#include "sysemu/sysemu.h"
#include "qemu/cutils.h"
+#include "qapi/error.h"
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
{
@@ -682,7 +683,7 @@ int net_slirp_smb(const char *exported_dir)
#endif /* !defined(_WIN32) */
struct GuestFwd {
- CharDriverState *hd;
+ CharBackend hd;
struct in_addr server;
int port;
Slirp *slirp;
@@ -746,15 +747,24 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str,
return -1;
}
} else {
- fwd = g_new(struct GuestFwd, 1);
- fwd->hd = qemu_chr_new(buf, p, NULL);
- if (!fwd->hd) {
+ Error *err = NULL;
+ CharDriverState *chr = qemu_chr_new(buf, p);
+
+ if (!chr) {
error_report("could not open guest forwarding device '%s'", buf);
+ return -1;
+ }
+
+ fwd = g_new(struct GuestFwd, 1);
+ qemu_chr_fe_init(&fwd->hd, chr, &err);
+ if (err) {
+ error_report_err(err);
g_free(fwd);
return -1;
}
- if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
+ if (slirp_add_exec(s->slirp, 3, qemu_chr_fe_get_driver(&fwd->hd),
+ &server, port) < 0) {
error_report("conflicting/invalid host:port in guest forwarding "
"rule '%s'", config_str);
g_free(fwd);
@@ -764,9 +774,8 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str,
fwd->port = port;
fwd->slirp = s->slirp;
- qemu_chr_fe_claim_no_fail(fwd->hd);
- qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
- NULL, fwd);
+ qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
+ NULL, fwd, NULL, true);
}
return 0;
diff --git a/net/vhost-user.c b/net/vhost-user.c
index 5b94c84541..7aff77ee4a 100644
--- a/net/vhost-user.c
+++ b/net/vhost-user.c
@@ -20,7 +20,7 @@
typedef struct VhostUserState {
NetClientState nc;
- CharDriverState *chr;
+ CharBackend chr; /* only queue index 0 */
VHostNetState *vhost_net;
guint watch;
uint64_t acked_features;
@@ -62,7 +62,7 @@ static void vhost_user_stop(int queues, NetClientState *ncs[])
}
}
-static int vhost_user_start(int queues, NetClientState *ncs[])
+static int vhost_user_start(int queues, NetClientState *ncs[], CharBackend *be)
{
VhostNetOptions options;
struct vhost_net *net = NULL;
@@ -78,7 +78,7 @@ static int vhost_user_start(int queues, NetClientState *ncs[])
s = DO_UPCAST(VhostUserState, nc, ncs[i]);
options.net_backend = ncs[i];
- options.opaque = s->chr;
+ options.opaque = be;
options.busyloop_timeout = 0;
net = vhost_net_init(&options);
if (!net) {
@@ -150,10 +150,8 @@ static void vhost_user_cleanup(NetClientState *nc)
g_free(s->vhost_net);
s->vhost_net = NULL;
}
- if (s->chr) {
- qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
- qemu_chr_fe_release(s->chr);
- s->chr = NULL;
+ if (nc->queue_index == 0) {
+ qemu_chr_fe_deinit(&s->chr);
}
qemu_purge_queued_packets(nc);
@@ -187,7 +185,7 @@ static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
{
VhostUserState *s = opaque;
- qemu_chr_disconnect(s->chr);
+ qemu_chr_fe_disconnect(&s->chr);
return FALSE;
}
@@ -197,6 +195,7 @@ static void net_vhost_user_event(void *opaque, int event)
const char *name = opaque;
NetClientState *ncs[MAX_QUEUE_NUM];
VhostUserState *s;
+ CharDriverState *chr;
Error *err = NULL;
int queues;
@@ -206,13 +205,14 @@ static void net_vhost_user_event(void *opaque, int event)
assert(queues < MAX_QUEUE_NUM);
s = DO_UPCAST(VhostUserState, nc, ncs[0]);
- trace_vhost_user_event(s->chr->label, event);
+ chr = qemu_chr_fe_get_driver(&s->chr);
+ trace_vhost_user_event(chr->label, event);
switch (event) {
case CHR_EVENT_OPENED:
- s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP,
+ s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
net_vhost_user_watch, s);
- if (vhost_user_start(queues, ncs) < 0) {
- qemu_chr_disconnect(s->chr);
+ if (vhost_user_start(queues, ncs, &s->chr) < 0) {
+ qemu_chr_fe_disconnect(&s->chr);
return;
}
qmp_set_link(name, true, &err);
@@ -235,6 +235,7 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
const char *name, CharDriverState *chr,
int queues)
{
+ Error *err = NULL;
NetClientState *nc, *nc0 = NULL;
VhostUserState *s;
int i;
@@ -244,28 +245,28 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
for (i = 0; i < queues; i++) {
nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
- if (!nc0) {
- nc0 = nc;
- }
-
snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
i, chr->label);
-
nc->queue_index = i;
+ if (!nc0) {
+ nc0 = nc;
+ s = DO_UPCAST(VhostUserState, nc, nc);
+ if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
+ error_report_err(err);
+ return -1;
+ }
+ }
- s = DO_UPCAST(VhostUserState, nc, nc);
- s->chr = chr;
}
s = DO_UPCAST(VhostUserState, nc, nc0);
do {
- Error *err = NULL;
- if (qemu_chr_wait_connected(chr, &err) < 0) {
+ if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
error_report_err(err);
return -1;
}
- qemu_chr_add_handlers(chr, NULL, NULL,
- net_vhost_user_event, nc0->name);
+ qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
+ net_vhost_user_event, nc0->name, NULL, true);
} while (!s->started);
assert(s->vhost_net);
@@ -294,8 +295,6 @@ static CharDriverState *net_vhost_claim_chardev(
return NULL;
}
- qemu_chr_fe_claim_no_fail(chr);
-
return chr;
}