Continue the main idea: avoid dependency on @tap in net_tap_setup(). So, move QAPI parsing to net_tap_new(). Move setting sndbuf to net_tap_set_fd(), as it's more appropriate place (other initial fd settings are here).
Note that net_tap_new() and net_tap_set_fd() are shared with net_init_bridge(), which didn't set sndbuf. Handle this case by sndbuf=0 (we never pass zero to tap_set_sndbuf(), so let this specific value mean that we don't want touch sndbuf). Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]> Reviewed-by: Maksim Davydov <[email protected]> --- net/tap.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/net/tap.c b/net/tap.c index f5830f4b00..b5ac856a3d 100644 --- a/net/tap.c +++ b/net/tap.c @@ -83,6 +83,9 @@ typedef struct TAPState { VHostNetState *vhost_net; unsigned host_vnet_hdr_len; Notifier exit; + + bool sndbuf_required; + int sndbuf; } TAPState; static void launch_script(const char *setup_script, const char *ifname, @@ -413,17 +416,25 @@ static NetClientInfo net_tap_info = { }; static TAPState *net_tap_new(NetClientState *peer, const char *model, - const char *name) + const char *name, const NetdevTapOptions *tap) { NetClientState *nc = qemu_new_net_client(&net_tap_info, peer, model, name); TAPState *s = DO_UPCAST(TAPState, nc, nc); s->fd = -1; + if (!tap) { + return s; + } + + s->sndbuf_required = tap->has_sndbuf; + s->sndbuf = + (tap->has_sndbuf && tap->sndbuf) ? MIN(tap->sndbuf, INT_MAX) : INT_MAX; + return s; } -static void net_tap_set_fd(TAPState *s, int fd, int vnet_hdr) +static bool net_tap_set_fd(TAPState *s, int fd, int vnet_hdr, Error **errp) { NetOffloads ol = {}; @@ -444,6 +455,15 @@ static void net_tap_set_fd(TAPState *s, int fd, int vnet_hdr) } tap_read_poll(s, true); s->vhost_net = NULL; + + if (s->sndbuf) { + Error **e = s->sndbuf_required ? errp : NULL; + if (!tap_set_sndbuf(s->fd, s->sndbuf, e) && s->sndbuf_required) { + return false; + } + } + + return true; } static void close_all_fds_after_fork(int excluded_fd) @@ -661,8 +681,8 @@ int net_init_bridge(const Netdev *netdev, const char *name, return -1; } - s = net_tap_new(peer, "bridge", name); - net_tap_set_fd(s, fd, vnet_hdr); + s = net_tap_new(peer, "bridge", name, NULL); + net_tap_set_fd(s, fd, vnet_hdr, &error_abort); qemu_set_info_str(&s->nc, "helper=%s,br=%s", helper, br); @@ -702,16 +722,10 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, const char *downscript, const char *vhostfdname, int vnet_hdr, int fd, Error **errp) { - TAPState *s = net_tap_new(peer, model, name); + TAPState *s = net_tap_new(peer, model, name, tap); int vhostfd; - bool sndbuf_required = tap->has_sndbuf; - int sndbuf = - (tap->has_sndbuf && tap->sndbuf) ? MIN(tap->sndbuf, INT_MAX) : INT_MAX; - - net_tap_set_fd(s, fd, vnet_hdr); - if (!tap_set_sndbuf(fd, sndbuf, sndbuf_required ? errp : NULL) && - sndbuf_required) { + if (!net_tap_set_fd(s, fd, vnet_hdr, errp)) { goto failed; } -- 2.48.1
