Il 18/10/2012 16:56, Stefan Hajnoczi ha scritto: > From: Stefan Hajnoczi <stefa...@linux.vnet.ibm.com> > > 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).
Could you use object_dynamic_cast(OBJECT(backend), TYPE_TAP_NET_CLIENT) instead? Paolo > 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 6737600..d60da6a 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_OPTIONS_KIND_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 8342391..6668ff2 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_OPTIONS_KIND_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_OPTIONS_KIND_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_OPTIONS_KIND_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_OPTIONS_KIND_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 df89caa..22f8de7 100644 > --- a/net/tap.c > +++ b/net/tap.c > @@ -304,6 +304,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_OPTIONS_KIND_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 d44d83a..4a9431b 100644 > --- a/net/tap.h > +++ b/net/tap.h > @@ -50,6 +50,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; >