dfb08dba49be1b5df0704df3d25ef0b8f125f082
[dpdk.git] / lib / librte_vhost / vhost.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <linux/vhost.h>
35 #include <linux/virtio_net.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #ifdef RTE_LIBRTE_VHOST_NUMA
40 #include <numaif.h>
41 #endif
42
43 #include <rte_ethdev.h>
44 #include <rte_log.h>
45 #include <rte_string_fns.h>
46 #include <rte_memory.h>
47 #include <rte_malloc.h>
48 #include <rte_virtio_net.h>
49
50 #include "vhost.h"
51
52 #define VHOST_USER_F_PROTOCOL_FEATURES  30
53
54 /* Features supported by this lib. */
55 #define VHOST_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
56                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
57                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
58                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
59                                 (1ULL << VIRTIO_NET_F_MQ)      | \
60                                 (1ULL << VIRTIO_F_VERSION_1)   | \
61                                 (1ULL << VHOST_F_LOG_ALL)      | \
62                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
63                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
64                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
65                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
66                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
67                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
68                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
69                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
70                                 (1ULL << VIRTIO_NET_F_MTU))
71
72 uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
73
74 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
75
76 /* device ops to add/remove device to/from data core. */
77 struct virtio_net_device_ops const *notify_ops;
78
79 struct virtio_net *
80 get_device(int vid)
81 {
82         struct virtio_net *dev = vhost_devices[vid];
83
84         if (unlikely(!dev)) {
85                 RTE_LOG(ERR, VHOST_CONFIG,
86                         "(%d) device not found.\n", vid);
87         }
88
89         return dev;
90 }
91
92 static void
93 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
94 {
95         if ((vq->callfd >= 0) && (destroy != 0))
96                 close(vq->callfd);
97         if (vq->kickfd >= 0)
98                 close(vq->kickfd);
99 }
100
101 /*
102  * Unmap any memory, close any file descriptors and
103  * free any memory owned by a device.
104  */
105 void
106 cleanup_device(struct virtio_net *dev, int destroy)
107 {
108         uint32_t i;
109
110         vhost_backend_cleanup(dev);
111
112         for (i = 0; i < dev->virt_qp_nb; i++) {
113                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ], destroy);
114                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ], destroy);
115         }
116 }
117
118 /*
119  * Release virtqueues and device memory.
120  */
121 static void
122 free_device(struct virtio_net *dev)
123 {
124         uint32_t i;
125         struct vhost_virtqueue *rxq, *txq;
126
127         for (i = 0; i < dev->virt_qp_nb; i++) {
128                 rxq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
129                 txq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
130
131                 rte_free(rxq->shadow_used_ring);
132                 rte_free(txq->shadow_used_ring);
133
134                 /* rxq and txq are allocated together as queue-pair */
135                 rte_free(rxq);
136         }
137
138         rte_free(dev);
139 }
140
141 static void
142 init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
143 {
144         memset(vq, 0, sizeof(struct vhost_virtqueue));
145
146         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
147         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
148
149         /* Backends are set to -1 indicating an inactive device. */
150         vq->backend = -1;
151
152         /* always set the default vq pair to enabled */
153         if (qp_idx == 0)
154                 vq->enabled = 1;
155
156         TAILQ_INIT(&vq->zmbuf_list);
157 }
158
159 static void
160 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
161 {
162         uint32_t base_idx = qp_idx * VIRTIO_QNUM;
163
164         init_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
165         init_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
166 }
167
168 static void
169 reset_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
170 {
171         int callfd;
172
173         callfd = vq->callfd;
174         init_vring_queue(vq, qp_idx);
175         vq->callfd = callfd;
176 }
177
178 static void
179 reset_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
180 {
181         uint32_t base_idx = qp_idx * VIRTIO_QNUM;
182
183         reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
184         reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
185 }
186
187 int
188 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
189 {
190         struct vhost_virtqueue *virtqueue = NULL;
191         uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
192         uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
193
194         virtqueue = rte_malloc(NULL,
195                                sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
196         if (virtqueue == NULL) {
197                 RTE_LOG(ERR, VHOST_CONFIG,
198                         "Failed to allocate memory for virt qp:%d.\n", qp_idx);
199                 return -1;
200         }
201
202         dev->virtqueue[virt_rx_q_idx] = virtqueue;
203         dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
204
205         init_vring_queue_pair(dev, qp_idx);
206
207         dev->virt_qp_nb += 1;
208
209         return 0;
210 }
211
212 /*
213  * Reset some variables in device structure, while keeping few
214  * others untouched, such as vid, ifname, virt_qp_nb: they
215  * should be same unless the device is removed.
216  */
217 void
218 reset_device(struct virtio_net *dev)
219 {
220         uint32_t i;
221
222         dev->features = 0;
223         dev->protocol_features = 0;
224         dev->flags = 0;
225
226         for (i = 0; i < dev->virt_qp_nb; i++)
227                 reset_vring_queue_pair(dev, i);
228 }
229
230 /*
231  * Invoked when there is a new vhost-user connection established (when
232  * there is a new virtio device being attached).
233  */
234 int
235 vhost_new_device(void)
236 {
237         struct virtio_net *dev;
238         int i;
239
240         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
241         if (dev == NULL) {
242                 RTE_LOG(ERR, VHOST_CONFIG,
243                         "Failed to allocate memory for new dev.\n");
244                 return -1;
245         }
246
247         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
248                 if (vhost_devices[i] == NULL)
249                         break;
250         }
251         if (i == MAX_VHOST_DEVICE) {
252                 RTE_LOG(ERR, VHOST_CONFIG,
253                         "Failed to find a free slot for new device.\n");
254                 rte_free(dev);
255                 return -1;
256         }
257
258         vhost_devices[i] = dev;
259         dev->vid = i;
260
261         return i;
262 }
263
264 /*
265  * Invoked when there is the vhost-user connection is broken (when
266  * the virtio device is being detached).
267  */
268 void
269 vhost_destroy_device(int vid)
270 {
271         struct virtio_net *dev = get_device(vid);
272
273         if (dev == NULL)
274                 return;
275
276         if (dev->flags & VIRTIO_DEV_RUNNING) {
277                 dev->flags &= ~VIRTIO_DEV_RUNNING;
278                 notify_ops->destroy_device(vid);
279         }
280
281         cleanup_device(dev, 1);
282         free_device(dev);
283
284         vhost_devices[vid] = NULL;
285 }
286
287 void
288 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
289 {
290         struct virtio_net *dev;
291         unsigned int len;
292
293         dev = get_device(vid);
294         if (dev == NULL)
295                 return;
296
297         len = if_len > sizeof(dev->ifname) ?
298                 sizeof(dev->ifname) : if_len;
299
300         strncpy(dev->ifname, if_name, len);
301         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
302 }
303
304 void
305 vhost_enable_dequeue_zero_copy(int vid)
306 {
307         struct virtio_net *dev = get_device(vid);
308
309         if (dev == NULL)
310                 return;
311
312         dev->dequeue_zero_copy = 1;
313 }
314
315 int
316 rte_vhost_get_mtu(int vid, uint16_t *mtu)
317 {
318         struct virtio_net *dev = get_device(vid);
319
320         if (!dev)
321                 return -ENODEV;
322
323         if (!(dev->flags & VIRTIO_DEV_READY))
324                 return -EAGAIN;
325
326         if (!(dev->features & VIRTIO_NET_F_MTU))
327                 return -ENOTSUP;
328
329         *mtu = dev->mtu;
330
331         return 0;
332 }
333
334 int
335 rte_vhost_get_numa_node(int vid)
336 {
337 #ifdef RTE_LIBRTE_VHOST_NUMA
338         struct virtio_net *dev = get_device(vid);
339         int numa_node;
340         int ret;
341
342         if (dev == NULL)
343                 return -1;
344
345         ret = get_mempolicy(&numa_node, NULL, 0, dev,
346                             MPOL_F_NODE | MPOL_F_ADDR);
347         if (ret < 0) {
348                 RTE_LOG(ERR, VHOST_CONFIG,
349                         "(%d) failed to query numa node: %d\n", vid, ret);
350                 return -1;
351         }
352
353         return numa_node;
354 #else
355         RTE_SET_USED(vid);
356         return -1;
357 #endif
358 }
359
360 uint32_t
361 rte_vhost_get_queue_num(int vid)
362 {
363         struct virtio_net *dev = get_device(vid);
364
365         if (dev == NULL)
366                 return 0;
367
368         return dev->virt_qp_nb;
369 }
370
371 int
372 rte_vhost_get_ifname(int vid, char *buf, size_t len)
373 {
374         struct virtio_net *dev = get_device(vid);
375
376         if (dev == NULL)
377                 return -1;
378
379         len = RTE_MIN(len, sizeof(dev->ifname));
380
381         strncpy(buf, dev->ifname, len);
382         buf[len - 1] = '\0';
383
384         return 0;
385 }
386
387 uint16_t
388 rte_vhost_avail_entries(int vid, uint16_t queue_id)
389 {
390         struct virtio_net *dev;
391         struct vhost_virtqueue *vq;
392
393         dev = get_device(vid);
394         if (!dev)
395                 return 0;
396
397         vq = dev->virtqueue[queue_id];
398         if (!vq->enabled)
399                 return 0;
400
401         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
402 }
403
404 int
405 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
406 {
407         struct virtio_net *dev = get_device(vid);
408
409         if (dev == NULL)
410                 return -1;
411
412         if (enable) {
413                 RTE_LOG(ERR, VHOST_CONFIG,
414                         "guest notification isn't supported.\n");
415                 return -1;
416         }
417
418         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
419         return 0;
420 }
421
422 uint64_t rte_vhost_feature_get(void)
423 {
424         return VHOST_FEATURES;
425 }
426
427 int rte_vhost_feature_disable(uint64_t feature_mask)
428 {
429         VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
430         return 0;
431 }
432
433 int rte_vhost_feature_enable(uint64_t feature_mask)
434 {
435         if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
436                 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
437                 return 0;
438         }
439         return -1;
440 }
441
442 /*
443  * Register ops so that we can add/remove device to data core.
444  */
445 int
446 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)
447 {
448         notify_ops = ops;
449
450         return 0;
451 }