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