c4ba437c44867c9ad6faa240ac457853de17f2be
[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
43 #include "vhost.h"
44 #include "virtio_user_dev.h"
45 #include "../virtio_ethdev.h"
46
47 static int
48 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
49 {
50         int callfd, kickfd;
51         struct vhost_vring_file file;
52         struct vhost_vring_state state;
53         struct vring *vring = &dev->vrings[queue_sel];
54         struct vhost_vring_addr addr = {
55                 .index = queue_sel,
56                 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
57                 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
58                 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
59                 .log_guest_addr = 0,
60                 .flags = 0, /* disable log */
61         };
62
63         /* May use invalid flag, but some backend leverages kickfd and callfd as
64          * criteria to judge if dev is alive. so finally we use real event_fd.
65          */
66         callfd = eventfd(0, O_CLOEXEC | O_NONBLOCK);
67         if (callfd < 0) {
68                 PMD_DRV_LOG(ERR, "callfd error, %s\n", strerror(errno));
69                 return -1;
70         }
71         kickfd = eventfd(0, O_CLOEXEC | O_NONBLOCK);
72         if (kickfd < 0) {
73                 close(callfd);
74                 PMD_DRV_LOG(ERR, "kickfd error, %s\n", strerror(errno));
75                 return -1;
76         }
77
78         /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
79          * firstly because vhost depends on this msg to allocate virtqueue
80          * pair.
81          */
82         file.index = queue_sel;
83         file.fd = callfd;
84         vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_CALL, &file);
85         dev->callfds[queue_sel] = callfd;
86
87         state.index = queue_sel;
88         state.num = vring->num;
89         vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_NUM, &state);
90
91         state.num = 0; /* no reservation */
92         vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_BASE, &state);
93
94         vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_ADDR, &addr);
95
96         /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
97          * lastly because vhost depends on this msg to judge if
98          * virtio is ready.
99          */
100         file.fd = kickfd;
101         vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_KICK, &file);
102         dev->kickfds[queue_sel] = kickfd;
103
104         return 0;
105 }
106
107 int
108 virtio_user_start_device(struct virtio_user_dev *dev)
109 {
110         uint64_t features;
111         uint32_t i, queue_sel;
112         int ret;
113
114         /* construct memory region inside each implementation */
115         ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_MEM_TABLE, NULL);
116         if (ret < 0)
117                 goto error;
118
119         for (i = 0; i < dev->max_queue_pairs; ++i) {
120                 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
121                 if (virtio_user_kick_queue(dev, queue_sel) < 0) {
122                         PMD_DRV_LOG(INFO, "kick rx vq fails: %u", i);
123                         goto error;
124                 }
125         }
126         for (i = 0; i < dev->max_queue_pairs; ++i) {
127                 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
128                 if (virtio_user_kick_queue(dev, queue_sel) < 0) {
129                         PMD_DRV_LOG(INFO, "kick tx vq fails: %u", i);
130                         goto error;
131                 }
132         }
133
134         /* After setup all virtqueues, we need to set_features so that
135          * these features can be set into each virtqueue in vhost side.
136          * And before that, make sure VIRTIO_NET_F_MAC is stripped.
137          */
138         features = dev->features;
139         features &= ~(1ull << VIRTIO_NET_F_MAC);
140         ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_FEATURES, &features);
141         if (ret < 0)
142                 goto error;
143         PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
144
145         return 0;
146 error:
147         /* TODO: free resource here or caller to check */
148         return -1;
149 }
150
151 int virtio_user_stop_device(struct virtio_user_dev *dev)
152 {
153         return vhost_user_sock(dev->vhostfd, VHOST_USER_RESET_OWNER, NULL);
154 }
155
156 static inline void
157 parse_mac(struct virtio_user_dev *dev, const char *mac)
158 {
159         int i, r;
160         uint32_t tmp[ETHER_ADDR_LEN];
161
162         if (!mac)
163                 return;
164
165         r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
166                         &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
167         if (r == ETHER_ADDR_LEN) {
168                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
169                         dev->mac_addr[i] = (uint8_t)tmp[i];
170                 dev->mac_specified = 1;
171         } else {
172                 /* ignore the wrong mac, use random mac */
173                 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
174         }
175 }
176
177 int
178 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
179                      int cq, int queue_size, const char *mac)
180 {
181         strncpy(dev->path, path, PATH_MAX);
182         dev->max_queue_pairs = queues;
183         dev->queue_pairs = 1; /* mq disabled by default */
184         dev->queue_size = queue_size;
185         dev->mac_specified = 0;
186         parse_mac(dev, mac);
187         dev->vhostfd = -1;
188         /* TODO: cq */
189         RTE_SET_USED(cq);
190
191         dev->vhostfd = vhost_user_setup(dev->path);
192         if (dev->vhostfd < 0) {
193                 PMD_INIT_LOG(ERR, "backend set up fails");
194                 return -1;
195         }
196         if (vhost_user_sock(dev->vhostfd, VHOST_USER_SET_OWNER, NULL) < 0) {
197                 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
198                 return -1;
199         }
200
201         if (vhost_user_sock(dev->vhostfd, VHOST_USER_GET_FEATURES,
202                             &dev->features) < 0) {
203                 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
204                 return -1;
205         }
206         if (dev->mac_specified)
207                 dev->features |= (1ull << VIRTIO_NET_F_MAC);
208         /* disable it until we support CQ */
209         dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
210         dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
211
212         return 0;
213 }
214
215 void
216 virtio_user_dev_uninit(struct virtio_user_dev *dev)
217 {
218         uint32_t i;
219
220         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
221                 close(dev->callfds[i]);
222                 close(dev->kickfds[i]);
223         }
224
225         close(dev->vhostfd);
226 }