79412714a30cfe1abad4e8a9d34166fd3d715dbb
[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
45 #include "vhost.h"
46 #include "virtio_user_dev.h"
47 #include "../virtio_ethdev.h"
48
49 static int
50 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
51 {
52         /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
53          * firstly because vhost depends on this msg to allocate virtqueue
54          * pair.
55          */
56         struct vhost_vring_file file;
57
58         file.index = queue_sel;
59         file.fd = dev->callfds[queue_sel];
60         dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
61
62         return 0;
63 }
64
65 static int
66 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
67 {
68         struct vhost_vring_file file;
69         struct vhost_vring_state state;
70         struct vring *vring = &dev->vrings[queue_sel];
71         struct vhost_vring_addr addr = {
72                 .index = queue_sel,
73                 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
74                 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
75                 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
76                 .log_guest_addr = 0,
77                 .flags = 0, /* disable log */
78         };
79
80         state.index = queue_sel;
81         state.num = vring->num;
82         dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
83
84         state.index = queue_sel;
85         state.num = 0; /* no reservation */
86         dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
87
88         dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
89
90         /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
91          * lastly because vhost depends on this msg to judge if
92          * virtio is ready.
93          */
94         file.index = queue_sel;
95         file.fd = dev->kickfds[queue_sel];
96         dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
97
98         return 0;
99 }
100
101 static int
102 virtio_user_queue_setup(struct virtio_user_dev *dev,
103                         int (*fn)(struct virtio_user_dev *, uint32_t))
104 {
105         uint32_t i, queue_sel;
106
107         for (i = 0; i < dev->max_queue_pairs; ++i) {
108                 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
109                 if (fn(dev, queue_sel) < 0) {
110                         PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
111                         return -1;
112                 }
113         }
114         for (i = 0; i < dev->max_queue_pairs; ++i) {
115                 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
116                 if (fn(dev, queue_sel) < 0) {
117                         PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
118                         return -1;
119                 }
120         }
121
122         return 0;
123 }
124
125 int
126 virtio_user_start_device(struct virtio_user_dev *dev)
127 {
128         uint64_t features;
129         int ret;
130
131         /* Step 0: tell vhost to create queues */
132         if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
133                 goto error;
134
135         /* Step 1: set features */
136         features = dev->features;
137         /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
138         features &= ~(1ull << VIRTIO_NET_F_MAC);
139         /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
140         features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
141         features &= ~(1ull << VIRTIO_NET_F_STATUS);
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_fill_intr_handle(struct virtio_user_dev *dev)
255 {
256         uint32_t i;
257         struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
258
259         if (!eth_dev->intr_handle) {
260                 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
261                 if (!eth_dev->intr_handle) {
262                         PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
263                         return -1;
264                 }
265                 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
266         }
267
268         for (i = 0; i < dev->max_queue_pairs; ++i)
269                 eth_dev->intr_handle->efds[i] = dev->callfds[i];
270         eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
271         eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
272         eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
273         if (dev->vhostfd >= 0)
274                 eth_dev->intr_handle->fd = dev->vhostfd;
275
276         return 0;
277 }
278
279 static int
280 virtio_user_dev_setup(struct virtio_user_dev *dev)
281 {
282         uint32_t q;
283
284         dev->vhostfd = -1;
285         dev->vhostfds = NULL;
286         dev->tapfds = NULL;
287
288         if (is_vhost_user_by_type(dev->path)) {
289                 dev->ops = &ops_user;
290         } else {
291                 dev->ops = &ops_kernel;
292
293                 dev->vhostfds = malloc(dev->max_queue_pairs * sizeof(int));
294                 dev->tapfds = malloc(dev->max_queue_pairs * sizeof(int));
295                 if (!dev->vhostfds || !dev->tapfds) {
296                         PMD_INIT_LOG(ERR, "Failed to malloc");
297                         return -1;
298                 }
299
300                 for (q = 0; q < dev->max_queue_pairs; ++q) {
301                         dev->vhostfds[q] = -1;
302                         dev->tapfds[q] = -1;
303                 }
304         }
305
306         if (dev->ops->setup(dev) < 0)
307                 return -1;
308
309         if (virtio_user_dev_init_notify(dev) < 0)
310                 return -1;
311
312         if (virtio_user_fill_intr_handle(dev) < 0)
313                 return -1;
314
315         return 0;
316 }
317
318 /* Use below macro to filter features from vhost backend */
319 #define VIRTIO_USER_SUPPORTED_FEATURES                  \
320         (1ULL << VIRTIO_NET_F_MAC               |       \
321          1ULL << VIRTIO_NET_F_STATUS            |       \
322          1ULL << VIRTIO_NET_F_MQ                |       \
323          1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR     |       \
324          1ULL << VIRTIO_NET_F_CTRL_VQ           |       \
325          1ULL << VIRTIO_NET_F_CTRL_RX           |       \
326          1ULL << VIRTIO_NET_F_CTRL_VLAN         |       \
327          1ULL << VIRTIO_NET_F_CSUM              |       \
328          1ULL << VIRTIO_NET_F_HOST_TSO4         |       \
329          1ULL << VIRTIO_NET_F_HOST_TSO6         |       \
330          1ULL << VIRTIO_NET_F_MRG_RXBUF         |       \
331          1ULL << VIRTIO_RING_F_INDIRECT_DESC    |       \
332          1ULL << VIRTIO_NET_F_GUEST_CSUM        |       \
333          1ULL << VIRTIO_NET_F_GUEST_TSO4        |       \
334          1ULL << VIRTIO_NET_F_GUEST_TSO6        |       \
335          1ULL << VIRTIO_F_VERSION_1)
336
337 int
338 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
339                      int cq, int queue_size, const char *mac, char **ifname)
340 {
341         snprintf(dev->path, PATH_MAX, "%s", path);
342         dev->max_queue_pairs = queues;
343         dev->queue_pairs = 1; /* mq disabled by default */
344         dev->queue_size = queue_size;
345         dev->mac_specified = 0;
346         parse_mac(dev, mac);
347
348         if (*ifname) {
349                 dev->ifname = *ifname;
350                 *ifname = NULL;
351         }
352
353         if (virtio_user_dev_setup(dev) < 0) {
354                 PMD_INIT_LOG(ERR, "backend set up fails");
355                 return -1;
356         }
357         if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL) < 0) {
358                 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
359                 return -1;
360         }
361
362         if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
363                             &dev->device_features) < 0) {
364                 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
365                 return -1;
366         }
367         if (dev->mac_specified)
368                 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
369
370         if (cq) {
371                 /* device does not really need to know anything about CQ,
372                  * so if necessary, we just claim to support CQ
373                  */
374                 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
375         } else {
376                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
377                 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
378                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
379                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
380                 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
381                 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
382                 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
383         }
384
385         /* The backend will not report this feature, we add it explicitly */
386         if (is_vhost_user_by_type(dev->path))
387                 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
388
389         dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
390
391         return 0;
392 }
393
394 void
395 virtio_user_dev_uninit(struct virtio_user_dev *dev)
396 {
397         uint32_t i;
398
399         virtio_user_stop_device(dev);
400
401         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
402                 close(dev->callfds[i]);
403                 close(dev->kickfds[i]);
404         }
405
406         close(dev->vhostfd);
407
408         if (dev->vhostfds) {
409                 for (i = 0; i < dev->max_queue_pairs; ++i)
410                         close(dev->vhostfds[i]);
411                 free(dev->vhostfds);
412                 free(dev->tapfds);
413         }
414
415         free(dev->ifname);
416 }
417
418 static uint8_t
419 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
420 {
421         uint16_t i;
422         uint8_t ret = 0;
423
424         if (q_pairs > dev->max_queue_pairs) {
425                 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
426                              q_pairs, dev->max_queue_pairs);
427                 return -1;
428         }
429
430         for (i = 0; i < q_pairs; ++i)
431                 ret |= dev->ops->enable_qp(dev, i, 1);
432         for (i = q_pairs; i < dev->max_queue_pairs; ++i)
433                 ret |= dev->ops->enable_qp(dev, i, 0);
434
435         dev->queue_pairs = q_pairs;
436
437         return ret;
438 }
439
440 static uint32_t
441 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
442                             uint16_t idx_hdr)
443 {
444         struct virtio_net_ctrl_hdr *hdr;
445         virtio_net_ctrl_ack status = ~0;
446         uint16_t i, idx_data, idx_status;
447         uint32_t n_descs = 0;
448
449         /* locate desc for header, data, and status */
450         idx_data = vring->desc[idx_hdr].next;
451         n_descs++;
452
453         i = idx_data;
454         while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
455                 i = vring->desc[i].next;
456                 n_descs++;
457         }
458
459         /* locate desc for status */
460         idx_status = i;
461         n_descs++;
462
463         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
464         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
465             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
466                 uint16_t queues;
467
468                 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
469                 status = virtio_user_handle_mq(dev, queues);
470         }
471
472         /* Update status */
473         *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
474
475         return n_descs;
476 }
477
478 void
479 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
480 {
481         uint16_t avail_idx, desc_idx;
482         struct vring_used_elem *uep;
483         uint32_t n_descs;
484         struct vring *vring = &dev->vrings[queue_idx];
485
486         /* Consume avail ring, using used ring idx as first one */
487         while (vring->used->idx != vring->avail->idx) {
488                 avail_idx = (vring->used->idx) & (vring->num - 1);
489                 desc_idx = vring->avail->ring[avail_idx];
490
491                 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
492
493                 /* Update used ring */
494                 uep = &vring->used->ring[avail_idx];
495                 uep->id = avail_idx;
496                 uep->len = n_descs;
497
498                 vring->used->idx++;
499         }
500 }