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