Statistics
| Branch: | Revision:

root / hw / vhost_net.c @ b0eb8449

History | View | Annotate | Download (6.4 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 35f75462 Michael Tokarev
#include "qemu-error.h"
19 d5970055 Michael S. Tsirkin
20 d5970055 Michael S. Tsirkin
#include "config.h"
21 d5970055 Michael S. Tsirkin
22 d5970055 Michael S. Tsirkin
#ifdef CONFIG_VHOST_NET
23 d5970055 Michael S. Tsirkin
#include <linux/vhost.h>
24 d5970055 Michael S. Tsirkin
#include <sys/socket.h>
25 d5970055 Michael S. Tsirkin
#include <linux/kvm.h>
26 d5970055 Michael S. Tsirkin
#include <fcntl.h>
27 d5970055 Michael S. Tsirkin
#include <sys/ioctl.h>
28 d5970055 Michael S. Tsirkin
#include <linux/virtio_ring.h>
29 d5970055 Michael S. Tsirkin
#include <netpacket/packet.h>
30 d5970055 Michael S. Tsirkin
#include <net/ethernet.h>
31 d5970055 Michael S. Tsirkin
#include <net/if.h>
32 d5970055 Michael S. Tsirkin
#include <netinet/in.h>
33 d5970055 Michael S. Tsirkin
34 d5970055 Michael S. Tsirkin
#include <stdio.h>
35 d5970055 Michael S. Tsirkin
36 d5970055 Michael S. Tsirkin
#include "vhost.h"
37 d5970055 Michael S. Tsirkin
38 d5970055 Michael S. Tsirkin
struct vhost_net {
39 d5970055 Michael S. Tsirkin
    struct vhost_dev dev;
40 d5970055 Michael S. Tsirkin
    struct vhost_virtqueue vqs[2];
41 d5970055 Michael S. Tsirkin
    int backend;
42 d5970055 Michael S. Tsirkin
    VLANClientState *vc;
43 d5970055 Michael S. Tsirkin
};
44 d5970055 Michael S. Tsirkin
45 d5970055 Michael S. Tsirkin
unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
46 d5970055 Michael S. Tsirkin
{
47 d5970055 Michael S. Tsirkin
    /* Clear features not supported by host kernel. */
48 d5970055 Michael S. Tsirkin
    if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
49 d5970055 Michael S. Tsirkin
        features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
50 d5970055 Michael S. Tsirkin
    }
51 d5970055 Michael S. Tsirkin
    if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
52 d5970055 Michael S. Tsirkin
        features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
53 d5970055 Michael S. Tsirkin
    }
54 bcbabae8 Michael S. Tsirkin
    if (!(net->dev.features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
55 bcbabae8 Michael S. Tsirkin
        features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
56 bcbabae8 Michael S. Tsirkin
    }
57 ca736c8e Michael S. Tsirkin
    if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
58 ca736c8e Michael S. Tsirkin
        features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
59 ca736c8e Michael S. Tsirkin
    }
60 d5970055 Michael S. Tsirkin
    return features;
61 d5970055 Michael S. Tsirkin
}
62 d5970055 Michael S. Tsirkin
63 d5970055 Michael S. Tsirkin
void vhost_net_ack_features(struct vhost_net *net, unsigned features)
64 d5970055 Michael S. Tsirkin
{
65 d5970055 Michael S. Tsirkin
    net->dev.acked_features = net->dev.backend_features;
66 d5970055 Michael S. Tsirkin
    if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
67 d5970055 Michael S. Tsirkin
        net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
68 d5970055 Michael S. Tsirkin
    }
69 d5970055 Michael S. Tsirkin
    if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
70 d5970055 Michael S. Tsirkin
        net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
71 d5970055 Michael S. Tsirkin
    }
72 bcbabae8 Michael S. Tsirkin
    if (features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
73 bcbabae8 Michael S. Tsirkin
        net->dev.acked_features |= (1 << VIRTIO_RING_F_EVENT_IDX);
74 bcbabae8 Michael S. Tsirkin
    }
75 ca736c8e Michael S. Tsirkin
    if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
76 ca736c8e Michael S. Tsirkin
        net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
77 ca736c8e Michael S. Tsirkin
    }
78 d5970055 Michael S. Tsirkin
}
79 d5970055 Michael S. Tsirkin
80 d5970055 Michael S. Tsirkin
static int vhost_net_get_fd(VLANClientState *backend)
81 d5970055 Michael S. Tsirkin
{
82 d5970055 Michael S. Tsirkin
    switch (backend->info->type) {
83 d5970055 Michael S. Tsirkin
    case NET_CLIENT_TYPE_TAP:
84 d5970055 Michael S. Tsirkin
        return tap_get_fd(backend);
85 d5970055 Michael S. Tsirkin
    default:
86 d5970055 Michael S. Tsirkin
        fprintf(stderr, "vhost-net requires tap backend\n");
87 d5970055 Michael S. Tsirkin
        return -EBADFD;
88 d5970055 Michael S. Tsirkin
    }
89 d5970055 Michael S. Tsirkin
}
90 d5970055 Michael S. Tsirkin
91 5430a28f mst@redhat.com
struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
92 5430a28f mst@redhat.com
                                 bool force)
93 d5970055 Michael S. Tsirkin
{
94 d5970055 Michael S. Tsirkin
    int r;
95 7267c094 Anthony Liguori
    struct vhost_net *net = g_malloc(sizeof *net);
96 d5970055 Michael S. Tsirkin
    if (!backend) {
97 d5970055 Michael S. Tsirkin
        fprintf(stderr, "vhost-net requires backend to be setup\n");
98 d5970055 Michael S. Tsirkin
        goto fail;
99 d5970055 Michael S. Tsirkin
    }
100 d5970055 Michael S. Tsirkin
    r = vhost_net_get_fd(backend);
101 d5970055 Michael S. Tsirkin
    if (r < 0) {
102 d5970055 Michael S. Tsirkin
        goto fail;
103 d5970055 Michael S. Tsirkin
    }
104 d5970055 Michael S. Tsirkin
    net->vc = backend;
105 d5970055 Michael S. Tsirkin
    net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
106 d5970055 Michael S. Tsirkin
        (1 << VHOST_NET_F_VIRTIO_NET_HDR);
107 d5970055 Michael S. Tsirkin
    net->backend = r;
108 d5970055 Michael S. Tsirkin
109 5430a28f mst@redhat.com
    r = vhost_dev_init(&net->dev, devfd, force);
110 d5970055 Michael S. Tsirkin
    if (r < 0) {
111 d5970055 Michael S. Tsirkin
        goto fail;
112 d5970055 Michael S. Tsirkin
    }
113 ca736c8e Michael S. Tsirkin
    if (!tap_has_vnet_hdr_len(backend,
114 ca736c8e Michael S. Tsirkin
                              sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
115 ca736c8e Michael S. Tsirkin
        net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
116 ca736c8e Michael S. Tsirkin
    }
117 d5970055 Michael S. Tsirkin
    if (~net->dev.features & net->dev.backend_features) {
118 0bfcd599 Blue Swirl
        fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
119 29f91781 Jes Sorensen
                (uint64_t)(~net->dev.features & net->dev.backend_features));
120 d5970055 Michael S. Tsirkin
        vhost_dev_cleanup(&net->dev);
121 d5970055 Michael S. Tsirkin
        goto fail;
122 d5970055 Michael S. Tsirkin
    }
123 d5970055 Michael S. Tsirkin
124 d5970055 Michael S. Tsirkin
    /* Set sane init value. Override when guest acks. */
125 d5970055 Michael S. Tsirkin
    vhost_net_ack_features(net, 0);
126 d5970055 Michael S. Tsirkin
    return net;
127 d5970055 Michael S. Tsirkin
fail:
128 7267c094 Anthony Liguori
    g_free(net);
129 d5970055 Michael S. Tsirkin
    return NULL;
130 d5970055 Michael S. Tsirkin
}
131 d5970055 Michael S. Tsirkin
132 5430a28f mst@redhat.com
bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
133 5430a28f mst@redhat.com
{
134 5430a28f mst@redhat.com
    return vhost_dev_query(&net->dev, dev);
135 5430a28f mst@redhat.com
}
136 5430a28f mst@redhat.com
137 d5970055 Michael S. Tsirkin
int vhost_net_start(struct vhost_net *net,
138 d5970055 Michael S. Tsirkin
                    VirtIODevice *dev)
139 d5970055 Michael S. Tsirkin
{
140 d5970055 Michael S. Tsirkin
    struct vhost_vring_file file = { };
141 d5970055 Michael S. Tsirkin
    int r;
142 b0b3db79 Michael S. Tsirkin
143 b0b3db79 Michael S. Tsirkin
    net->dev.nvqs = 2;
144 b0b3db79 Michael S. Tsirkin
    net->dev.vqs = net->vqs;
145 b0b3db79 Michael S. Tsirkin
146 b0b3db79 Michael S. Tsirkin
    r = vhost_dev_enable_notifiers(&net->dev, dev);
147 b0b3db79 Michael S. Tsirkin
    if (r < 0) {
148 b0b3db79 Michael S. Tsirkin
        goto fail_notifiers;
149 b0b3db79 Michael S. Tsirkin
    }
150 ca736c8e Michael S. Tsirkin
    if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
151 ca736c8e Michael S. Tsirkin
        tap_set_vnet_hdr_len(net->vc,
152 ca736c8e Michael S. Tsirkin
                             sizeof(struct virtio_net_hdr_mrg_rxbuf));
153 ca736c8e Michael S. Tsirkin
    }
154 d5970055 Michael S. Tsirkin
155 d5970055 Michael S. Tsirkin
    r = vhost_dev_start(&net->dev, dev);
156 d5970055 Michael S. Tsirkin
    if (r < 0) {
157 b0b3db79 Michael S. Tsirkin
        goto fail_start;
158 d5970055 Michael S. Tsirkin
    }
159 d5970055 Michael S. Tsirkin
160 d5970055 Michael S. Tsirkin
    net->vc->info->poll(net->vc, false);
161 d5970055 Michael S. Tsirkin
    qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
162 d5970055 Michael S. Tsirkin
    file.fd = net->backend;
163 d5970055 Michael S. Tsirkin
    for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
164 d5970055 Michael S. Tsirkin
        r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
165 d5970055 Michael S. Tsirkin
        if (r < 0) {
166 d5970055 Michael S. Tsirkin
            r = -errno;
167 d5970055 Michael S. Tsirkin
            goto fail;
168 d5970055 Michael S. Tsirkin
        }
169 d5970055 Michael S. Tsirkin
    }
170 d5970055 Michael S. Tsirkin
    return 0;
171 d5970055 Michael S. Tsirkin
fail:
172 d5970055 Michael S. Tsirkin
    file.fd = -1;
173 6b37c87c Michael S. Tsirkin
    while (file.index-- > 0) {
174 d5970055 Michael S. Tsirkin
        int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
175 d5970055 Michael S. Tsirkin
        assert(r >= 0);
176 d5970055 Michael S. Tsirkin
    }
177 d5970055 Michael S. Tsirkin
    net->vc->info->poll(net->vc, true);
178 d5970055 Michael S. Tsirkin
    vhost_dev_stop(&net->dev, dev);
179 ca736c8e Michael S. Tsirkin
    if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
180 ca736c8e Michael S. Tsirkin
        tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
181 ca736c8e Michael S. Tsirkin
    }
182 b0b3db79 Michael S. Tsirkin
fail_start:
183 b0b3db79 Michael S. Tsirkin
    vhost_dev_disable_notifiers(&net->dev, dev);
184 b0b3db79 Michael S. Tsirkin
fail_notifiers:
185 d5970055 Michael S. Tsirkin
    return r;
186 d5970055 Michael S. Tsirkin
}
187 d5970055 Michael S. Tsirkin
188 d5970055 Michael S. Tsirkin
void vhost_net_stop(struct vhost_net *net,
189 d5970055 Michael S. Tsirkin
                    VirtIODevice *dev)
190 d5970055 Michael S. Tsirkin
{
191 d5970055 Michael S. Tsirkin
    struct vhost_vring_file file = { .fd = -1 };
192 d5970055 Michael S. Tsirkin
193 d5970055 Michael S. Tsirkin
    for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
194 d5970055 Michael S. Tsirkin
        int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
195 d5970055 Michael S. Tsirkin
        assert(r >= 0);
196 d5970055 Michael S. Tsirkin
    }
197 d5970055 Michael S. Tsirkin
    net->vc->info->poll(net->vc, true);
198 d5970055 Michael S. Tsirkin
    vhost_dev_stop(&net->dev, dev);
199 ca736c8e Michael S. Tsirkin
    if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
200 ca736c8e Michael S. Tsirkin
        tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
201 ca736c8e Michael S. Tsirkin
    }
202 b0b3db79 Michael S. Tsirkin
    vhost_dev_disable_notifiers(&net->dev, dev);
203 d5970055 Michael S. Tsirkin
}
204 d5970055 Michael S. Tsirkin
205 d5970055 Michael S. Tsirkin
void vhost_net_cleanup(struct vhost_net *net)
206 d5970055 Michael S. Tsirkin
{
207 d5970055 Michael S. Tsirkin
    vhost_dev_cleanup(&net->dev);
208 ca736c8e Michael S. Tsirkin
    if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
209 ca736c8e Michael S. Tsirkin
        tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
210 ca736c8e Michael S. Tsirkin
    }
211 7267c094 Anthony Liguori
    g_free(net);
212 d5970055 Michael S. Tsirkin
}
213 d5970055 Michael S. Tsirkin
#else
214 5430a28f mst@redhat.com
struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
215 5430a28f mst@redhat.com
                                 bool force)
216 5430a28f mst@redhat.com
{
217 35f75462 Michael Tokarev
    error_report("vhost-net support is not compiled in");
218 5430a28f mst@redhat.com
    return NULL;
219 5430a28f mst@redhat.com
}
220 5430a28f mst@redhat.com
221 5430a28f mst@redhat.com
bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
222 d5970055 Michael S. Tsirkin
{
223 5430a28f mst@redhat.com
    return false;
224 d5970055 Michael S. Tsirkin
}
225 d5970055 Michael S. Tsirkin
226 d5970055 Michael S. Tsirkin
int vhost_net_start(struct vhost_net *net,
227 d5970055 Michael S. Tsirkin
                    VirtIODevice *dev)
228 d5970055 Michael S. Tsirkin
{
229 5430a28f mst@redhat.com
    return -ENOSYS;
230 d5970055 Michael S. Tsirkin
}
231 d5970055 Michael S. Tsirkin
void vhost_net_stop(struct vhost_net *net,
232 d5970055 Michael S. Tsirkin
                    VirtIODevice *dev)
233 d5970055 Michael S. Tsirkin
{
234 d5970055 Michael S. Tsirkin
}
235 d5970055 Michael S. Tsirkin
236 d5970055 Michael S. Tsirkin
void vhost_net_cleanup(struct vhost_net *net)
237 d5970055 Michael S. Tsirkin
{
238 d5970055 Michael S. Tsirkin
}
239 d5970055 Michael S. Tsirkin
240 d5970055 Michael S. Tsirkin
unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
241 d5970055 Michael S. Tsirkin
{
242 5430a28f mst@redhat.com
    return features;
243 d5970055 Michael S. Tsirkin
}
244 d5970055 Michael S. Tsirkin
void vhost_net_ack_features(struct vhost_net *net, unsigned features)
245 d5970055 Michael S. Tsirkin
{
246 d5970055 Michael S. Tsirkin
}
247 d5970055 Michael S. Tsirkin
#endif