The vhost-net code interacts closely with the net/tap.c backend so that it can pass the underlying file descriptor to the vhost_net.ko driver. We need a check that confirms a NetClientState is indeed a tap backend (and not something else like slirp or socket).
Formalize this in the new net_is_tap_client() function. This way callers do not reach inside net internals to check the type. Note that I've added this function to net/tap.c but named it net_is_tap_client(). Ideally it should be in net.c but I want to keep the code where the tap net client is defined. Signed-off-by: Stefan Hajnoczi <stefa...@linux.vnet.ibm.com> --- hw/vhost_net.c | 5 ++--- hw/virtio-net.c | 9 +++++---- net/tap.c | 5 +++++ net/tap.h | 1 + 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/hw/vhost_net.c b/hw/vhost_net.c index ac3e616..47710ee 100644 --- a/hw/vhost_net.c +++ b/hw/vhost_net.c @@ -82,10 +82,9 @@ void vhost_net_ack_features(struct vhost_net *net, unsigned features) static int vhost_net_get_fd(NetClientState *backend) { - switch (backend->info->type) { - case NET_CLIENT_TYPE_TAP: + if (net_is_tap_client(backend)) { return tap_get_fd(backend); - default: + } else { fprintf(stderr, "vhost-net requires tap backend\n"); return -EBADFD; } diff --git a/hw/virtio-net.c b/hw/virtio-net.c index d5527d4..1575879 100644 --- a/hw/virtio-net.c +++ b/hw/virtio-net.c @@ -108,7 +108,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) if (!n->nic->nc.peer) { return; } - if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) { + if (!net_is_tap_client(n->nic->nc.peer)) { return; } @@ -205,8 +205,9 @@ static int peer_has_vnet_hdr(VirtIONet *n) if (!n->nic->nc.peer) return 0; - if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) + if (!net_is_tap_client(n->nic->nc.peer)) { return 0; + } n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer); @@ -249,7 +250,7 @@ static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features) } if (!n->nic->nc.peer || - n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) { + !net_is_tap_client(n->nic->nc.peer)) { return features; } if (!tap_get_vhost_net(n->nic->nc.peer)) { @@ -288,7 +289,7 @@ static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) (features >> VIRTIO_NET_F_GUEST_UFO) & 1); } if (!n->nic->nc.peer || - n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) { + !net_is_tap_client(n->nic->nc.peer)) { return; } if (!tap_get_vhost_net(n->nic->nc.peer)) { diff --git a/net/tap.c b/net/tap.c index 20ce3ff..d5bdfa0 100644 --- a/net/tap.c +++ b/net/tap.c @@ -303,6 +303,11 @@ static void tap_poll(NetClientState *nc, bool enable) tap_write_poll(s, enable); } +bool net_is_tap_client(NetClientState *nc) +{ + return nc->info->type == NET_CLIENT_TYPE_TAP; +} + int tap_get_fd(NetClientState *nc) { TAPState *s = DO_UPCAST(TAPState, nc, nc); diff --git a/net/tap.h b/net/tap.h index 2966d81..9a341bf 100644 --- a/net/tap.h +++ b/net/tap.h @@ -52,6 +52,7 @@ int tap_probe_has_ufo(int fd); void tap_fd_set_offload(int fd, int csum, int tso4, int tso6, int ecn, int ufo); void tap_fd_set_vnet_hdr_len(int fd, int len); +bool net_is_tap_client(NetClientState *nc); int tap_get_fd(NetClientState *nc); struct vhost_net; -- 1.7.10