Statistics
| Branch: | Revision:

root / hw / vhost_net.c @ 8e00128d

History | View | Annotate | Download (4.6 kB)

1 d5970055 Michael S. Tsirkin
/*
2 d5970055 Michael S. Tsirkin
 * vhost-net support
3 d5970055 Michael S. Tsirkin
 *
4 d5970055 Michael S. Tsirkin
 * Copyright Red Hat, Inc. 2010
5 d5970055 Michael S. Tsirkin
 *
6 d5970055 Michael S. Tsirkin
 * Authors:
7 d5970055 Michael S. Tsirkin
 *  Michael S. Tsirkin <mst@redhat.com>
8 d5970055 Michael S. Tsirkin
 *
9 d5970055 Michael S. Tsirkin
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 d5970055 Michael S. Tsirkin
 * the COPYING file in the top-level directory.
11 d5970055 Michael S. Tsirkin
 */
12 d5970055 Michael S. Tsirkin
13 d5970055 Michael S. Tsirkin
#include "net.h"
14 d5970055 Michael S. Tsirkin
#include "net/tap.h"
15 d5970055 Michael S. Tsirkin
16 d5970055 Michael S. Tsirkin
#include "virtio-net.h"
17 d5970055 Michael S. Tsirkin
#include "vhost_net.h"
18 d5970055 Michael S. Tsirkin
19 d5970055 Michael S. Tsirkin
#include "config.h"
20 d5970055 Michael S. Tsirkin
21 d5970055 Michael S. Tsirkin
#ifdef CONFIG_VHOST_NET
22 d5970055 Michael S. Tsirkin
#include <linux/vhost.h>
23 d5970055 Michael S. Tsirkin
#include <sys/socket.h>
24 d5970055 Michael S. Tsirkin
#include <linux/kvm.h>
25 d5970055 Michael S. Tsirkin
#include <fcntl.h>
26 d5970055 Michael S. Tsirkin
#include <sys/ioctl.h>
27 d5970055 Michael S. Tsirkin
#include <linux/virtio_ring.h>
28 d5970055 Michael S. Tsirkin
#include <netpacket/packet.h>
29 d5970055 Michael S. Tsirkin
#include <net/ethernet.h>
30 d5970055 Michael S. Tsirkin
#include <net/if.h>
31 d5970055 Michael S. Tsirkin
#include <netinet/in.h>
32 d5970055 Michael S. Tsirkin
33 d5970055 Michael S. Tsirkin
#include <stdio.h>
34 d5970055 Michael S. Tsirkin
35 d5970055 Michael S. Tsirkin
#include "vhost.h"
36 d5970055 Michael S. Tsirkin
37 d5970055 Michael S. Tsirkin
struct vhost_net {
38 d5970055 Michael S. Tsirkin
    struct vhost_dev dev;
39 d5970055 Michael S. Tsirkin
    struct vhost_virtqueue vqs[2];
40 d5970055 Michael S. Tsirkin
    int backend;
41 d5970055 Michael S. Tsirkin
    VLANClientState *vc;
42 d5970055 Michael S. Tsirkin
};
43 d5970055 Michael S. Tsirkin
44 d5970055 Michael S. Tsirkin
unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
45 d5970055 Michael S. Tsirkin
{
46 d5970055 Michael S. Tsirkin
    /* Clear features not supported by host kernel. */
47 d5970055 Michael S. Tsirkin
    if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
48 d5970055 Michael S. Tsirkin
        features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
49 d5970055 Michael S. Tsirkin
    }
50 d5970055 Michael S. Tsirkin
    if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
51 d5970055 Michael S. Tsirkin
        features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
52 d5970055 Michael S. Tsirkin
    }
53 5751995a Michael S. Tsirkin
    features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
54 d5970055 Michael S. Tsirkin
    return features;
55 d5970055 Michael S. Tsirkin
}
56 d5970055 Michael S. Tsirkin
57 d5970055 Michael S. Tsirkin
void vhost_net_ack_features(struct vhost_net *net, unsigned features)
58 d5970055 Michael S. Tsirkin
{
59 d5970055 Michael S. Tsirkin
    net->dev.acked_features = net->dev.backend_features;
60 d5970055 Michael S. Tsirkin
    if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
61 d5970055 Michael S. Tsirkin
        net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
62 d5970055 Michael S. Tsirkin
    }
63 d5970055 Michael S. Tsirkin
    if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
64 d5970055 Michael S. Tsirkin
        net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
65 d5970055 Michael S. Tsirkin
    }
66 d5970055 Michael S. Tsirkin
}
67 d5970055 Michael S. Tsirkin
68 d5970055 Michael S. Tsirkin
static int vhost_net_get_fd(VLANClientState *backend)
69 d5970055 Michael S. Tsirkin
{
70 d5970055 Michael S. Tsirkin
    switch (backend->info->type) {
71 d5970055 Michael S. Tsirkin
    case NET_CLIENT_TYPE_TAP:
72 d5970055 Michael S. Tsirkin
        return tap_get_fd(backend);
73 d5970055 Michael S. Tsirkin
    default:
74 d5970055 Michael S. Tsirkin
        fprintf(stderr, "vhost-net requires tap backend\n");
75 d5970055 Michael S. Tsirkin
        return -EBADFD;
76 d5970055 Michael S. Tsirkin
    }
77 d5970055 Michael S. Tsirkin
}
78 d5970055 Michael S. Tsirkin
79 d5970055 Michael S. Tsirkin
struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd)
80 d5970055 Michael S. Tsirkin
{
81 d5970055 Michael S. Tsirkin
    int r;
82 d5970055 Michael S. Tsirkin
    struct vhost_net *net = qemu_malloc(sizeof *net);
83 d5970055 Michael S. Tsirkin
    if (!backend) {
84 d5970055 Michael S. Tsirkin
        fprintf(stderr, "vhost-net requires backend to be setup\n");
85 d5970055 Michael S. Tsirkin
        goto fail;
86 d5970055 Michael S. Tsirkin
    }
87 d5970055 Michael S. Tsirkin
    r = vhost_net_get_fd(backend);
88 d5970055 Michael S. Tsirkin
    if (r < 0) {
89 d5970055 Michael S. Tsirkin
        goto fail;
90 d5970055 Michael S. Tsirkin
    }
91 d5970055 Michael S. Tsirkin
    net->vc = backend;
92 d5970055 Michael S. Tsirkin
    net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
93 d5970055 Michael S. Tsirkin
        (1 << VHOST_NET_F_VIRTIO_NET_HDR);
94 d5970055 Michael S. Tsirkin
    net->backend = r;
95 d5970055 Michael S. Tsirkin
96 d5970055 Michael S. Tsirkin
    r = vhost_dev_init(&net->dev, devfd);
97 d5970055 Michael S. Tsirkin
    if (r < 0) {
98 d5970055 Michael S. Tsirkin
        goto fail;
99 d5970055 Michael S. Tsirkin
    }
100 d5970055 Michael S. Tsirkin
    if (~net->dev.features & net->dev.backend_features) {
101 0bfcd599 Blue Swirl
        fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
102 29f91781 Jes Sorensen
                (uint64_t)(~net->dev.features & net->dev.backend_features));
103 d5970055 Michael S. Tsirkin
        vhost_dev_cleanup(&net->dev);
104 d5970055 Michael S. Tsirkin
        goto fail;
105 d5970055 Michael S. Tsirkin
    }
106 d5970055 Michael S. Tsirkin
107 d5970055 Michael S. Tsirkin
    /* Set sane init value. Override when guest acks. */
108 d5970055 Michael S. Tsirkin
    vhost_net_ack_features(net, 0);
109 d5970055 Michael S. Tsirkin
    return net;
110 d5970055 Michael S. Tsirkin
fail:
111 d5970055 Michael S. Tsirkin
    qemu_free(net);
112 d5970055 Michael S. Tsirkin
    return NULL;
113 d5970055 Michael S. Tsirkin
}
114 d5970055 Michael S. Tsirkin
115 d5970055 Michael S. Tsirkin
int vhost_net_start(struct vhost_net *net,
116 d5970055 Michael S. Tsirkin
                    VirtIODevice *dev)
117 d5970055 Michael S. Tsirkin
{
118 d5970055 Michael S. Tsirkin
    struct vhost_vring_file file = { };
119 d5970055 Michael S. Tsirkin
    int r;
120 d5970055 Michael S. Tsirkin
121 d5970055 Michael S. Tsirkin
    net->dev.nvqs = 2;
122 d5970055 Michael S. Tsirkin
    net->dev.vqs = net->vqs;
123 d5970055 Michael S. Tsirkin
    r = vhost_dev_start(&net->dev, dev);
124 d5970055 Michael S. Tsirkin
    if (r < 0) {
125 d5970055 Michael S. Tsirkin
        return r;
126 d5970055 Michael S. Tsirkin
    }
127 d5970055 Michael S. Tsirkin
128 d5970055 Michael S. Tsirkin
    net->vc->info->poll(net->vc, false);
129 d5970055 Michael S. Tsirkin
    qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
130 d5970055 Michael S. Tsirkin
    file.fd = net->backend;
131 d5970055 Michael S. Tsirkin
    for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
132 d5970055 Michael S. Tsirkin
        r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
133 d5970055 Michael S. Tsirkin
        if (r < 0) {
134 d5970055 Michael S. Tsirkin
            r = -errno;
135 d5970055 Michael S. Tsirkin
            goto fail;
136 d5970055 Michael S. Tsirkin
        }
137 d5970055 Michael S. Tsirkin
    }
138 d5970055 Michael S. Tsirkin
    return 0;
139 d5970055 Michael S. Tsirkin
fail:
140 d5970055 Michael S. Tsirkin
    file.fd = -1;
141 d5970055 Michael S. Tsirkin
    while (--file.index >= 0) {
142 d5970055 Michael S. Tsirkin
        int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
143 d5970055 Michael S. Tsirkin
        assert(r >= 0);
144 d5970055 Michael S. Tsirkin
    }
145 d5970055 Michael S. Tsirkin
    net->vc->info->poll(net->vc, true);
146 d5970055 Michael S. Tsirkin
    vhost_dev_stop(&net->dev, dev);
147 d5970055 Michael S. Tsirkin
    return r;
148 d5970055 Michael S. Tsirkin
}
149 d5970055 Michael S. Tsirkin
150 d5970055 Michael S. Tsirkin
void vhost_net_stop(struct vhost_net *net,
151 d5970055 Michael S. Tsirkin
                    VirtIODevice *dev)
152 d5970055 Michael S. Tsirkin
{
153 d5970055 Michael S. Tsirkin
    struct vhost_vring_file file = { .fd = -1 };
154 d5970055 Michael S. Tsirkin
155 d5970055 Michael S. Tsirkin
    for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
156 d5970055 Michael S. Tsirkin
        int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
157 d5970055 Michael S. Tsirkin
        assert(r >= 0);
158 d5970055 Michael S. Tsirkin
    }
159 d5970055 Michael S. Tsirkin
    net->vc->info->poll(net->vc, true);
160 d5970055 Michael S. Tsirkin
    vhost_dev_stop(&net->dev, dev);
161 d5970055 Michael S. Tsirkin
}
162 d5970055 Michael S. Tsirkin
163 d5970055 Michael S. Tsirkin
void vhost_net_cleanup(struct vhost_net *net)
164 d5970055 Michael S. Tsirkin
{
165 d5970055 Michael S. Tsirkin
    vhost_dev_cleanup(&net->dev);
166 d5970055 Michael S. Tsirkin
    qemu_free(net);
167 d5970055 Michael S. Tsirkin
}
168 d5970055 Michael S. Tsirkin
#else
169 d5970055 Michael S. Tsirkin
struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd)
170 d5970055 Michael S. Tsirkin
{
171 d5970055 Michael S. Tsirkin
        return NULL;
172 d5970055 Michael S. Tsirkin
}
173 d5970055 Michael S. Tsirkin
174 d5970055 Michael S. Tsirkin
int vhost_net_start(struct vhost_net *net,
175 d5970055 Michael S. Tsirkin
                    VirtIODevice *dev)
176 d5970055 Michael S. Tsirkin
{
177 d5970055 Michael S. Tsirkin
        return -ENOSYS;
178 d5970055 Michael S. Tsirkin
}
179 d5970055 Michael S. Tsirkin
void vhost_net_stop(struct vhost_net *net,
180 d5970055 Michael S. Tsirkin
                    VirtIODevice *dev)
181 d5970055 Michael S. Tsirkin
{
182 d5970055 Michael S. Tsirkin
}
183 d5970055 Michael S. Tsirkin
184 d5970055 Michael S. Tsirkin
void vhost_net_cleanup(struct vhost_net *net)
185 d5970055 Michael S. Tsirkin
{
186 d5970055 Michael S. Tsirkin
}
187 d5970055 Michael S. Tsirkin
188 d5970055 Michael S. Tsirkin
unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
189 d5970055 Michael S. Tsirkin
{
190 d5970055 Michael S. Tsirkin
        return features;
191 d5970055 Michael S. Tsirkin
}
192 d5970055 Michael S. Tsirkin
void vhost_net_ack_features(struct vhost_net *net, unsigned features)
193 d5970055 Michael S. Tsirkin
{
194 d5970055 Michael S. Tsirkin
}
195 d5970055 Michael S. Tsirkin
#endif