7b40a922a61421da102f5b2fffbf446340a838d2
[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 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
53
54 /* device ops to add/remove device to/from data core. */
55 struct virtio_net_device_ops const *notify_ops;
56
57 struct virtio_net *
58 get_device(int vid)
59 {
60         struct virtio_net *dev = vhost_devices[vid];
61
62         if (unlikely(!dev)) {
63                 RTE_LOG(ERR, VHOST_CONFIG,
64                         "(%d) device not found.\n", vid);
65         }
66
67         return dev;
68 }
69
70 static void
71 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
72 {
73         if ((vq->callfd >= 0) && (destroy != 0))
74                 close(vq->callfd);
75         if (vq->kickfd >= 0)
76                 close(vq->kickfd);
77 }
78
79 /*
80  * Unmap any memory, close any file descriptors and
81  * free any memory owned by a device.
82  */
83 void
84 cleanup_device(struct virtio_net *dev, int destroy)
85 {
86         uint32_t i;
87
88         vhost_backend_cleanup(dev);
89
90         for (i = 0; i < dev->virt_qp_nb; i++) {
91                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ], destroy);
92                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ], destroy);
93         }
94 }
95
96 /*
97  * Release virtqueues and device memory.
98  */
99 static void
100 free_device(struct virtio_net *dev)
101 {
102         uint32_t i;
103         struct vhost_virtqueue *rxq, *txq;
104
105         for (i = 0; i < dev->virt_qp_nb; i++) {
106                 rxq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
107                 txq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
108
109                 rte_free(rxq->shadow_used_ring);
110                 rte_free(txq->shadow_used_ring);
111
112                 /* rxq and txq are allocated together as queue-pair */
113                 rte_free(rxq);
114         }
115
116         rte_free(dev);
117 }
118
119 static void
120 init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
121 {
122         memset(vq, 0, sizeof(struct vhost_virtqueue));
123
124         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
125         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
126
127         /* Backends are set to -1 indicating an inactive device. */
128         vq->backend = -1;
129
130         /* always set the default vq pair to enabled */
131         if (qp_idx == 0)
132                 vq->enabled = 1;
133
134         TAILQ_INIT(&vq->zmbuf_list);
135 }
136
137 static void
138 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
139 {
140         uint32_t base_idx = qp_idx * VIRTIO_QNUM;
141
142         init_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
143         init_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
144 }
145
146 static void
147 reset_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
148 {
149         int callfd;
150
151         callfd = vq->callfd;
152         init_vring_queue(vq, qp_idx);
153         vq->callfd = callfd;
154 }
155
156 static void
157 reset_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
158 {
159         uint32_t base_idx = qp_idx * VIRTIO_QNUM;
160
161         reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
162         reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
163 }
164
165 int
166 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
167 {
168         struct vhost_virtqueue *virtqueue = NULL;
169         uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
170         uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
171
172         virtqueue = rte_malloc(NULL,
173                                sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
174         if (virtqueue == NULL) {
175                 RTE_LOG(ERR, VHOST_CONFIG,
176                         "Failed to allocate memory for virt qp:%d.\n", qp_idx);
177                 return -1;
178         }
179
180         dev->virtqueue[virt_rx_q_idx] = virtqueue;
181         dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
182
183         init_vring_queue_pair(dev, qp_idx);
184
185         dev->virt_qp_nb += 1;
186
187         return 0;
188 }
189
190 /*
191  * Reset some variables in device structure, while keeping few
192  * others untouched, such as vid, ifname, virt_qp_nb: they
193  * should be same unless the device is removed.
194  */
195 void
196 reset_device(struct virtio_net *dev)
197 {
198         uint32_t i;
199
200         dev->features = 0;
201         dev->protocol_features = 0;
202         dev->flags = 0;
203
204         for (i = 0; i < dev->virt_qp_nb; i++)
205                 reset_vring_queue_pair(dev, i);
206 }
207
208 /*
209  * Invoked when there is a new vhost-user connection established (when
210  * there is a new virtio device being attached).
211  */
212 int
213 vhost_new_device(void)
214 {
215         struct virtio_net *dev;
216         int i;
217
218         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
219         if (dev == NULL) {
220                 RTE_LOG(ERR, VHOST_CONFIG,
221                         "Failed to allocate memory for new dev.\n");
222                 return -1;
223         }
224
225         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
226                 if (vhost_devices[i] == NULL)
227                         break;
228         }
229         if (i == MAX_VHOST_DEVICE) {
230                 RTE_LOG(ERR, VHOST_CONFIG,
231                         "Failed to find a free slot for new device.\n");
232                 rte_free(dev);
233                 return -1;
234         }
235
236         vhost_devices[i] = dev;
237         dev->vid = i;
238
239         return i;
240 }
241
242 /*
243  * Invoked when there is the vhost-user connection is broken (when
244  * the virtio device is being detached).
245  */
246 void
247 vhost_destroy_device(int vid)
248 {
249         struct virtio_net *dev = get_device(vid);
250
251         if (dev == NULL)
252                 return;
253
254         if (dev->flags & VIRTIO_DEV_RUNNING) {
255                 dev->flags &= ~VIRTIO_DEV_RUNNING;
256                 notify_ops->destroy_device(vid);
257         }
258
259         cleanup_device(dev, 1);
260         free_device(dev);
261
262         vhost_devices[vid] = NULL;
263 }
264
265 void
266 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
267 {
268         struct virtio_net *dev;
269         unsigned int len;
270
271         dev = get_device(vid);
272         if (dev == NULL)
273                 return;
274
275         len = if_len > sizeof(dev->ifname) ?
276                 sizeof(dev->ifname) : if_len;
277
278         strncpy(dev->ifname, if_name, len);
279         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
280 }
281
282 void
283 vhost_enable_dequeue_zero_copy(int vid)
284 {
285         struct virtio_net *dev = get_device(vid);
286
287         if (dev == NULL)
288                 return;
289
290         dev->dequeue_zero_copy = 1;
291 }
292
293 int
294 rte_vhost_get_mtu(int vid, uint16_t *mtu)
295 {
296         struct virtio_net *dev = get_device(vid);
297
298         if (!dev)
299                 return -ENODEV;
300
301         if (!(dev->flags & VIRTIO_DEV_READY))
302                 return -EAGAIN;
303
304         if (!(dev->features & VIRTIO_NET_F_MTU))
305                 return -ENOTSUP;
306
307         *mtu = dev->mtu;
308
309         return 0;
310 }
311
312 int
313 rte_vhost_get_numa_node(int vid)
314 {
315 #ifdef RTE_LIBRTE_VHOST_NUMA
316         struct virtio_net *dev = get_device(vid);
317         int numa_node;
318         int ret;
319
320         if (dev == NULL)
321                 return -1;
322
323         ret = get_mempolicy(&numa_node, NULL, 0, dev,
324                             MPOL_F_NODE | MPOL_F_ADDR);
325         if (ret < 0) {
326                 RTE_LOG(ERR, VHOST_CONFIG,
327                         "(%d) failed to query numa node: %d\n", vid, ret);
328                 return -1;
329         }
330
331         return numa_node;
332 #else
333         RTE_SET_USED(vid);
334         return -1;
335 #endif
336 }
337
338 uint32_t
339 rte_vhost_get_queue_num(int vid)
340 {
341         struct virtio_net *dev = get_device(vid);
342
343         if (dev == NULL)
344                 return 0;
345
346         return dev->virt_qp_nb;
347 }
348
349 int
350 rte_vhost_get_ifname(int vid, char *buf, size_t len)
351 {
352         struct virtio_net *dev = get_device(vid);
353
354         if (dev == NULL)
355                 return -1;
356
357         len = RTE_MIN(len, sizeof(dev->ifname));
358
359         strncpy(buf, dev->ifname, len);
360         buf[len - 1] = '\0';
361
362         return 0;
363 }
364
365 uint16_t
366 rte_vhost_avail_entries(int vid, uint16_t queue_id)
367 {
368         struct virtio_net *dev;
369         struct vhost_virtqueue *vq;
370
371         dev = get_device(vid);
372         if (!dev)
373                 return 0;
374
375         vq = dev->virtqueue[queue_id];
376         if (!vq->enabled)
377                 return 0;
378
379         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
380 }
381
382 int
383 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
384 {
385         struct virtio_net *dev = get_device(vid);
386
387         if (dev == NULL)
388                 return -1;
389
390         if (enable) {
391                 RTE_LOG(ERR, VHOST_CONFIG,
392                         "guest notification isn't supported.\n");
393                 return -1;
394         }
395
396         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
397         return 0;
398 }
399
400 /*
401  * Register ops so that we can add/remove device to data core.
402  */
403 int
404 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)
405 {
406         notify_ops = ops;
407
408         return 0;
409 }