net/virtio_user: fix first queue pair without multiqueue
[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, EFD_CLOEXEC | EFD_NONBLOCK);
67         if (callfd < 0) {
68                 PMD_DRV_LOG(ERR, "callfd error, %s\n", strerror(errno));
69                 return -1;
70         }
71         kickfd = eventfd(0, EFD_CLOEXEC | EFD_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         /* we enable the 1st queue pair by default. */
135         vhost_user_enable_queue_pair(dev->vhostfd, 0, 1);
136
137         /* After setup all virtqueues, we need to set_features so that these
138          * features can be set into each virtqueue in vhost side. And before
139          * that, make sure VHOST_USER_F_PROTOCOL_FEATURES is added if mq is
140          * enabled, and VIRTIO_NET_F_MAC is stripped.
141          */
142         features = dev->features;
143         if (dev->max_queue_pairs > 1)
144                 features |= VHOST_USER_MQ;
145         features &= ~(1ull << VIRTIO_NET_F_MAC);
146         ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_FEATURES, &features);
147         if (ret < 0)
148                 goto error;
149         PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
150
151         return 0;
152 error:
153         /* TODO: free resource here or caller to check */
154         return -1;
155 }
156
157 int virtio_user_stop_device(struct virtio_user_dev *dev)
158 {
159         return vhost_user_sock(dev->vhostfd, VHOST_USER_RESET_OWNER, NULL);
160 }
161
162 static inline void
163 parse_mac(struct virtio_user_dev *dev, const char *mac)
164 {
165         int i, r;
166         uint32_t tmp[ETHER_ADDR_LEN];
167
168         if (!mac)
169                 return;
170
171         r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
172                         &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
173         if (r == ETHER_ADDR_LEN) {
174                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
175                         dev->mac_addr[i] = (uint8_t)tmp[i];
176                 dev->mac_specified = 1;
177         } else {
178                 /* ignore the wrong mac, use random mac */
179                 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
180         }
181 }
182
183 int
184 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
185                      int cq, int queue_size, const char *mac)
186 {
187         snprintf(dev->path, PATH_MAX, "%s", path);
188         dev->max_queue_pairs = queues;
189         dev->queue_pairs = 1; /* mq disabled by default */
190         dev->queue_size = queue_size;
191         dev->mac_specified = 0;
192         parse_mac(dev, mac);
193         dev->vhostfd = -1;
194
195         dev->vhostfd = vhost_user_setup(dev->path);
196         if (dev->vhostfd < 0) {
197                 PMD_INIT_LOG(ERR, "backend set up fails");
198                 return -1;
199         }
200         if (vhost_user_sock(dev->vhostfd, VHOST_USER_SET_OWNER, NULL) < 0) {
201                 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
202                 return -1;
203         }
204
205         if (vhost_user_sock(dev->vhostfd, VHOST_USER_GET_FEATURES,
206                             &dev->features) < 0) {
207                 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
208                 return -1;
209         }
210         if (dev->mac_specified)
211                 dev->features |= (1ull << VIRTIO_NET_F_MAC);
212
213         if (!cq) {
214                 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
215                 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
216                 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
217                 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
218                 dev->features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
219                 dev->features &= ~(1ull << VIRTIO_NET_F_MQ);
220                 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
221         } else {
222                 /* vhost user backend does not need to know ctrl-q, so
223                  * actually we need add this bit into features. However,
224                  * DPDK vhost-user does send features with this bit, so we
225                  * check it instead of OR it for now.
226                  */
227                 if (!(dev->features & (1ull << VIRTIO_NET_F_CTRL_VQ)))
228                         PMD_INIT_LOG(INFO, "vhost does not support ctrl-q");
229         }
230
231         if (dev->max_queue_pairs > 1) {
232                 if (!(dev->features & VHOST_USER_MQ)) {
233                         PMD_INIT_LOG(ERR, "MQ not supported by the backend");
234                         return -1;
235                 }
236         }
237
238         return 0;
239 }
240
241 void
242 virtio_user_dev_uninit(struct virtio_user_dev *dev)
243 {
244         uint32_t i;
245
246         for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
247                 close(dev->callfds[i]);
248                 close(dev->kickfds[i]);
249         }
250
251         close(dev->vhostfd);
252 }
253
254 static uint8_t
255 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
256 {
257         uint16_t i;
258         uint8_t ret = 0;
259
260         if (q_pairs > dev->max_queue_pairs) {
261                 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
262                              q_pairs, dev->max_queue_pairs);
263                 return -1;
264         }
265
266         for (i = 0; i < q_pairs; ++i)
267                 ret |= vhost_user_enable_queue_pair(dev->vhostfd, i, 1);
268         for (i = q_pairs; i < dev->max_queue_pairs; ++i)
269                 ret |= vhost_user_enable_queue_pair(dev->vhostfd, i, 0);
270
271         dev->queue_pairs = q_pairs;
272
273         return ret;
274 }
275
276 static uint32_t
277 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
278                             uint16_t idx_hdr)
279 {
280         struct virtio_net_ctrl_hdr *hdr;
281         virtio_net_ctrl_ack status = ~0;
282         uint16_t i, idx_data, idx_status;
283         uint32_t n_descs = 0;
284
285         /* locate desc for header, data, and status */
286         idx_data = vring->desc[idx_hdr].next;
287         n_descs++;
288
289         i = idx_data;
290         while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
291                 i = vring->desc[i].next;
292                 n_descs++;
293         }
294
295         /* locate desc for status */
296         idx_status = i;
297         n_descs++;
298
299         hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
300         if (hdr->class == VIRTIO_NET_CTRL_MQ &&
301             hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
302                 uint16_t queues;
303
304                 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
305                 status = virtio_user_handle_mq(dev, queues);
306         }
307
308         /* Update status */
309         *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
310
311         return n_descs;
312 }
313
314 void
315 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
316 {
317         uint16_t avail_idx, desc_idx;
318         struct vring_used_elem *uep;
319         uint32_t n_descs;
320         struct vring *vring = &dev->vrings[queue_idx];
321
322         /* Consume avail ring, using used ring idx as first one */
323         while (vring->used->idx != vring->avail->idx) {
324                 avail_idx = (vring->used->idx) & (vring->num - 1);
325                 desc_idx = vring->avail->ring[avail_idx];
326
327                 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
328
329                 /* Update used ring */
330                 uep = &vring->used->ring[avail_idx];
331                 uep->id = avail_idx;
332                 uep->len = n_descs;
333
334                 vring->used->idx++;
335         }
336 }