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