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