net/virtio-user: move eventfd open/close into init/uninit
[dpdk.git] / drivers / net / virtio / virtio_user / virtio_user_dev.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 <stdint.h>
35 #include <stdio.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/mman.h>
40 #include <unistd.h>
41 #include <sys/eventfd.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45
46 #include "vhost.h"
47 #include "virtio_user_dev.h"
48 #include "../virtio_ethdev.h"
49
50 static int
51 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
52 {
53         /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
54          * firstly because vhost depends on this msg to allocate virtqueue
55          * pair.
56          */
57         struct vhost_vring_file file;
58
59         file.index = queue_sel;
60         file.fd = dev->callfds[queue_sel];
61         dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
62
63         return 0;
64 }
65
66 static int
67 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
68 {
69         struct vhost_vring_file file;
70         struct vhost_vring_state state;
71         struct vring *vring = &dev->vrings[queue_sel];
72         struct vhost_vring_addr addr = {
73                 .index = queue_sel,
74                 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
75                 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
76                 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
77                 .log_guest_addr = 0,
78                 .flags = 0, /* disable log */
79         };
80
81         state.index = queue_sel;
82         state.num = vring->num;
83         dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
84
85         state.index = queue_sel;
86         state.num = 0; /* no reservation */
87         dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
88
89         dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
90
91         /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
92          * lastly because vhost depends on this msg to judge if
93          * virtio is ready.
94          */
95         file.index = queue_sel;
96         file.fd = dev->kickfds[queue_sel];
97         dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
98
99         return 0;
100 }
101
102 static int
103 virtio_user_queue_setup(struct virtio_user_dev *dev,
104                         int (*fn)(struct virtio_user_dev *, uint32_t))
105 {
106         uint32_t i, queue_sel;
107
108         for (i = 0; i < dev->max_queue_pairs; ++i) {
109                 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
110                 if (fn(dev, queue_sel) < 0) {
111                         PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
112                         return -1;
113                 }
114         }
115         for (i = 0; i < dev->max_queue_pairs; ++i) {
116                 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
117                 if (fn(dev, queue_sel) < 0) {
118                         PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
119                         return -1;
120                 }
121         }
122
123         return 0;
124 }
125
126 int
127 virtio_user_start_device(struct virtio_user_dev *dev)
128 {
129         uint64_t features;
130         int ret;
131
132         /* Step 0: tell vhost to create queues */
133         if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
134                 goto error;
135
136         /* Step 1: set features */
137         features = dev->features;
138         /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
139         features &= ~(1ull << VIRTIO_NET_F_MAC);
140         /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
141         features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
142         ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
143         if (ret < 0)
144                 goto error;
145         PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
146
147         /* Step 2: share memory regions */
148         ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
149         if (ret < 0)
150                 goto error;
151
152         /* Step 3: kick queues */
153         if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
154                 goto error;
155
156         /* Step 4: enable queues
157          * we enable the 1st queue pair by default.
158          */
159         dev->ops->enable_qp(dev, 0, 1);
160
161         return 0;
162 error:
163         /* TODO: free resource here or caller to check */
164         return -1;
165 }
166
167 int virtio_user_stop_device(struct virtio_user_dev *dev)
168 {
169         uint32_t i;
170
171         for (i = 0; i < dev->max_queue_pairs; ++i)
172                 dev->ops->enable_qp(dev, i, 0);
173
174         return 0;
175 }
176
177 static inline void
178 parse_mac(struct virtio_user_dev *dev, const char *mac)
179 {
180         int i, r;
181         uint32_t tmp[ETHER_ADDR_LEN];
182
183         if (!mac)
184                 return;
185
186         r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
187                         &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
188         if (r == ETHER_ADDR_LEN) {
189                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
190                         dev->mac_addr[i] = (uint8_t)tmp[i];
191                 dev->mac_specified = 1;
192         } else {
193                 /* ignore the wrong mac, use random mac */
194                 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
195         }
196 }
197
198 int
199 is_vhost_user_by_type(const char *path)
200 {
201         struct stat sb;
202
203         if (stat(path, &sb) == -1)
204                 return 0;
205
206         return S_ISSOCK(sb.st_mode);
207 }
208
209 static int
210 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
211 {
212         uint32_t i, j;
213         int callfd;
214         int kickfd;
215
216         for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
217                 if (i >= dev->max_queue_pairs * 2) {
218                         dev->kickfds[i] = -1;
219                         dev->callfds[i] = -1;
220                         continue;
221                 }
222
223                 /* May use invalid flag, but some backend uses kickfd and
224                  * callfd as criteria to judge if dev is alive. so finally we
225                  * use real event_fd.
226                  */
227                 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
228                 if (callfd < 0) {
229                         PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
230                         break;
231                 }
232                 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
233                 if (kickfd < 0) {
234                         PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
235                         break;
236                 }
237                 dev->callfds[i] = callfd;
238                 dev->kickfds[i] = kickfd;
239         }
240
241         if (i < VIRTIO_MAX_VIRTQUEUES) {
242                 for (j = 0; j <= i; ++j) {
243                         close(dev->callfds[j]);
244                         close(dev->kickfds[j]);
245                 }
246
247                 return -1;
248         }
249
250         return 0;
251 }
252
253 static int
254 virtio_user_dev_setup(struct virtio_user_dev *dev)
255 {
256         uint32_t q;
257
258         dev->vhostfd = -1;
259         dev->vhostfds = NULL;
260         dev->tapfds = NULL;
261
262         if (virtio_user_dev_init_notify(dev) < 0)
263                 return -1;
264
265         if (is_vhost_user_by_type(dev->path)) {
266                 dev->ops = &ops_user;
267         } else {
268                 dev->ops = &ops_kernel;
269
270                 dev->vhostfds = malloc(dev->max_queue_pairs * sizeof(int));
271                 dev->tapfds = malloc(dev->max_queue_pairs * sizeof(int));
272                 if (!dev->vhostfds || !dev->tapfds) {
273                         PMD_INIT_LOG(ERR, "Failed to malloc");
274                         return -1;
275                 }
276
277                 for (q = 0; q < dev->max_queue_pairs; ++q) {
278                         dev->vhostfds[q] = -1;
279                         dev->tapfds[q] = -1;
280                 }
281         }
282
283         return dev->ops->setup(dev);
284 }
285
286 int
287 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
288                      int cq, int queue_size, const char *mac, char **ifname)
289 {
290         snprintf(dev->path, PATH_MAX, "%s", path);
291         dev->max_queue_pairs = queues;
292         dev->queue_pairs = 1; /* mq disabled by default */
293         dev->queue_size = queue_size;
294         dev->mac_specified = 0;
295         parse_mac(dev, mac);
296
297         if (*ifname) {
298                 dev->ifname = *ifname;
299                 *ifname = NULL;
300         }
301
302         if (virtio_user_dev_setup(dev) < 0) {
303                 PMD_INIT_LOG(ERR, "backend set up fails");
304                 return -1;
305         }
306         if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL) < 0) {
307                 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
308                 return -1;
309         }
310
311         if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
312                             &dev->device_features) < 0) {
313                 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
314                 return -1;
315         }
316         if (dev->mac_specified)
317                 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
318
319         if (cq) {
320                 /* device does not really need to know anything about CQ,
321                  * so if necessary, we just claim to support CQ
322                  */
323                 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
324         } else {
325                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
326                 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
327                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
328                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
329                 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
330                 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
331                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
332         }
333
334         return 0;
335 }
336
337 void
338 virtio_user_dev_uninit(struct virtio_user_dev *dev)
339 {
340         uint32_t i;
341
342         virtio_user_stop_device(dev);
343
344         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
345                 close(dev->callfds[i]);
346                 close(dev->kickfds[i]);
347         }
348
349         close(dev->vhostfd);
350
351         if (dev->vhostfds) {
352                 for (i = 0; i < dev->max_queue_pairs; ++i)
353                         close(dev->vhostfds[i]);
354                 free(dev->vhostfds);
355                 free(dev->tapfds);
356         }
357
358         free(dev->ifname);
359 }
360
361 static uint8_t
362 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
363 {
364         uint16_t i;
365         uint8_t ret = 0;
366
367         if (q_pairs > dev->max_queue_pairs) {
368                 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
369                              q_pairs, dev->max_queue_pairs);
370                 return -1;
371         }
372
373         for (i = 0; i < q_pairs; ++i)
374                 ret |= dev->ops->enable_qp(dev, i, 1);
375         for (i = q_pairs; i < dev->max_queue_pairs; ++i)
376                 ret |= dev->ops->enable_qp(dev, i, 0);
377
378         dev->queue_pairs = q_pairs;
379
380         return ret;
381 }
382
383 static uint32_t
384 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
385                             uint16_t idx_hdr)
386 {
387         struct virtio_net_ctrl_hdr *hdr;
388         virtio_net_ctrl_ack status = ~0;
389         uint16_t i, idx_data, idx_status;
390         uint32_t n_descs = 0;
391
392         /* locate desc for header, data, and status */
393         idx_data = vring->desc[idx_hdr].next;
394         n_descs++;
395
396         i = idx_data;
397         while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
398                 i = vring->desc[i].next;
399                 n_descs++;
400         }
401
402         /* locate desc for status */
403         idx_status = i;
404         n_descs++;
405
406         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
407         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
408             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
409                 uint16_t queues;
410
411                 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
412                 status = virtio_user_handle_mq(dev, queues);
413         }
414
415         /* Update status */
416         *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
417
418         return n_descs;
419 }
420
421 void
422 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
423 {
424         uint16_t avail_idx, desc_idx;
425         struct vring_used_elem *uep;
426         uint32_t n_descs;
427         struct vring *vring = &dev->vrings[queue_idx];
428
429         /* Consume avail ring, using used ring idx as first one */
430         while (vring->used->idx != vring->avail->idx) {
431                 avail_idx = (vring->used->idx) & (vring->num - 1);
432                 desc_idx = vring->avail->ring[avail_idx];
433
434                 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
435
436                 /* Update used ring */
437                 uep = &vring->used->ring[avail_idx];
438                 uep->id = avail_idx;
439                 uep->len = n_descs;
440
441                 vring->used->idx++;
442         }
443 }