@@ -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;
}
@@ -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)) {
@@ -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);
@@ -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;
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 <stefanha@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(-)