Statistics
| Branch: | Revision:

root / hw / vhost_net.c @ bcbabae8

History | View | Annotate | Download (6.1 kB)

1
/*
2
 * vhost-net support
3
 *
4
 * Copyright Red Hat, Inc. 2010
5
 *
6
 * Authors:
7
 *  Michael S. Tsirkin <mst@redhat.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 */
12

    
13
#include "net.h"
14
#include "net/tap.h"
15

    
16
#include "virtio-net.h"
17
#include "vhost_net.h"
18

    
19
#include "config.h"
20

    
21
#ifdef CONFIG_VHOST_NET
22
#include <linux/vhost.h>
23
#include <sys/socket.h>
24
#include <linux/kvm.h>
25
#include <fcntl.h>
26
#include <sys/ioctl.h>
27
#include <linux/virtio_ring.h>
28
#include <netpacket/packet.h>
29
#include <net/ethernet.h>
30
#include <net/if.h>
31
#include <netinet/in.h>
32

    
33
#include <stdio.h>
34

    
35
#include "vhost.h"
36

    
37
struct vhost_net {
38
    struct vhost_dev dev;
39
    struct vhost_virtqueue vqs[2];
40
    int backend;
41
    VLANClientState *vc;
42
};
43

    
44
unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
45
{
46
    /* Clear features not supported by host kernel. */
47
    if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
48
        features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
49
    }
50
    if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
51
        features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
52
    }
53
    if (!(net->dev.features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
54
        features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
55
    }
56
    if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
57
        features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
58
    }
59
    return features;
60
}
61

    
62
void vhost_net_ack_features(struct vhost_net *net, unsigned features)
63
{
64
    net->dev.acked_features = net->dev.backend_features;
65
    if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
66
        net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
67
    }
68
    if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
69
        net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
70
    }
71
    if (features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
72
        net->dev.acked_features |= (1 << VIRTIO_RING_F_EVENT_IDX);
73
    }
74
    if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
75
        net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
76
    }
77
}
78

    
79
static int vhost_net_get_fd(VLANClientState *backend)
80
{
81
    switch (backend->info->type) {
82
    case NET_CLIENT_TYPE_TAP:
83
        return tap_get_fd(backend);
84
    default:
85
        fprintf(stderr, "vhost-net requires tap backend\n");
86
        return -EBADFD;
87
    }
88
}
89

    
90
struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
91
                                 bool force)
92
{
93
    int r;
94
    struct vhost_net *net = qemu_malloc(sizeof *net);
95
    if (!backend) {
96
        fprintf(stderr, "vhost-net requires backend to be setup\n");
97
        goto fail;
98
    }
99
    r = vhost_net_get_fd(backend);
100
    if (r < 0) {
101
        goto fail;
102
    }
103
    net->vc = backend;
104
    net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
105
        (1 << VHOST_NET_F_VIRTIO_NET_HDR);
106
    net->backend = r;
107

    
108
    r = vhost_dev_init(&net->dev, devfd, force);
109
    if (r < 0) {
110
        goto fail;
111
    }
112
    if (!tap_has_vnet_hdr_len(backend,
113
                              sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
114
        net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
115
    }
116
    if (~net->dev.features & net->dev.backend_features) {
117
        fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
118
                (uint64_t)(~net->dev.features & net->dev.backend_features));
119
        vhost_dev_cleanup(&net->dev);
120
        goto fail;
121
    }
122

    
123
    /* Set sane init value. Override when guest acks. */
124
    vhost_net_ack_features(net, 0);
125
    return net;
126
fail:
127
    qemu_free(net);
128
    return NULL;
129
}
130

    
131
bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
132
{
133
    return vhost_dev_query(&net->dev, dev);
134
}
135

    
136
int vhost_net_start(struct vhost_net *net,
137
                    VirtIODevice *dev)
138
{
139
    struct vhost_vring_file file = { };
140
    int r;
141
    if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
142
        tap_set_vnet_hdr_len(net->vc,
143
                             sizeof(struct virtio_net_hdr_mrg_rxbuf));
144
    }
145

    
146
    net->dev.nvqs = 2;
147
    net->dev.vqs = net->vqs;
148
    r = vhost_dev_start(&net->dev, dev);
149
    if (r < 0) {
150
        return r;
151
    }
152

    
153
    net->vc->info->poll(net->vc, false);
154
    qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
155
    file.fd = net->backend;
156
    for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
157
        r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
158
        if (r < 0) {
159
            r = -errno;
160
            goto fail;
161
        }
162
    }
163
    return 0;
164
fail:
165
    file.fd = -1;
166
    while (file.index-- > 0) {
167
        int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
168
        assert(r >= 0);
169
    }
170
    net->vc->info->poll(net->vc, true);
171
    vhost_dev_stop(&net->dev, dev);
172
    if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
173
        tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
174
    }
175
    return r;
176
}
177

    
178
void vhost_net_stop(struct vhost_net *net,
179
                    VirtIODevice *dev)
180
{
181
    struct vhost_vring_file file = { .fd = -1 };
182

    
183
    for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
184
        int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
185
        assert(r >= 0);
186
    }
187
    net->vc->info->poll(net->vc, true);
188
    vhost_dev_stop(&net->dev, dev);
189
    if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
190
        tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
191
    }
192
}
193

    
194
void vhost_net_cleanup(struct vhost_net *net)
195
{
196
    vhost_dev_cleanup(&net->dev);
197
    if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
198
        tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
199
    }
200
    qemu_free(net);
201
}
202
#else
203
struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
204
                                 bool force)
205
{
206
    return NULL;
207
}
208

    
209
bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
210
{
211
    return false;
212
}
213

    
214
int vhost_net_start(struct vhost_net *net,
215
                    VirtIODevice *dev)
216
{
217
    return -ENOSYS;
218
}
219
void vhost_net_stop(struct vhost_net *net,
220
                    VirtIODevice *dev)
221
{
222
}
223

    
224
void vhost_net_cleanup(struct vhost_net *net)
225
{
226
}
227

    
228
unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
229
{
230
    return features;
231
}
232
void vhost_net_ack_features(struct vhost_net *net, unsigned features)
233
{
234
}
235
#endif