net/virtio-user: fix wrongly get/set features
[dpdk.git] / drivers / net / virtio / virtio_user_ethdev.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 <sys/types.h>
36 #include <unistd.h>
37
38 #include <rte_malloc.h>
39 #include <rte_kvargs.h>
40 #include <rte_vdev.h>
41
42 #include "virtio_ethdev.h"
43 #include "virtio_logs.h"
44 #include "virtio_pci.h"
45 #include "virtqueue.h"
46 #include "virtio_rxtx.h"
47 #include "virtio_user/virtio_user_dev.h"
48
49 #define virtio_user_get_dev(hw) \
50         ((struct virtio_user_dev *)(hw)->virtio_user_dev)
51
52 static void
53 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
54                      void *dst, int length)
55 {
56         int i;
57         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
58
59         if (offset == offsetof(struct virtio_net_config, mac) &&
60             length == ETHER_ADDR_LEN) {
61                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
62                         ((uint8_t *)dst)[i] = dev->mac_addr[i];
63                 return;
64         }
65
66         if (offset == offsetof(struct virtio_net_config, status))
67                 *(uint16_t *)dst = dev->status;
68
69         if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
70                 *(uint16_t *)dst = dev->max_queue_pairs;
71 }
72
73 static void
74 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
75                       const void *src, int length)
76 {
77         int i;
78         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
79
80         if ((offset == offsetof(struct virtio_net_config, mac)) &&
81             (length == ETHER_ADDR_LEN))
82                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
83                         dev->mac_addr[i] = ((const uint8_t *)src)[i];
84         else
85                 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d\n",
86                             offset, length);
87 }
88
89 static void
90 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
91 {
92         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
93
94         if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
95                 virtio_user_start_device(dev);
96         dev->status = status;
97 }
98
99 static void
100 virtio_user_reset(struct virtio_hw *hw)
101 {
102         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
103
104         virtio_user_stop_device(dev);
105 }
106
107 static uint8_t
108 virtio_user_get_status(struct virtio_hw *hw)
109 {
110         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
111
112         return dev->status;
113 }
114
115 static uint64_t
116 virtio_user_get_features(struct virtio_hw *hw)
117 {
118         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
119
120         /* unmask feature bits defined in vhost user protocol */
121         return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
122 }
123
124 static void
125 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
126 {
127         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
128
129         dev->features = features & dev->device_features;
130 }
131
132 static uint8_t
133 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
134 {
135         /* When config interrupt happens, driver calls this function to query
136          * what kinds of change happen. Interrupt mode not supported for now.
137          */
138         return 0;
139 }
140
141 static uint16_t
142 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
143                     uint16_t vec __rte_unused)
144 {
145         return VIRTIO_MSI_NO_VECTOR;
146 }
147
148 /* This function is to get the queue size, aka, number of descs, of a specified
149  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
150  * max supported queues.
151  */
152 static uint16_t
153 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
154 {
155         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
156
157         /* Currently, each queue has same queue size */
158         return dev->queue_size;
159 }
160
161 static int
162 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
163 {
164         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
165         uint16_t queue_idx = vq->vq_queue_index;
166         uint64_t desc_addr, avail_addr, used_addr;
167
168         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
169         avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
170         used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
171                                                          ring[vq->vq_nentries]),
172                                    VIRTIO_PCI_VRING_ALIGN);
173
174         dev->vrings[queue_idx].num = vq->vq_nentries;
175         dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
176         dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
177         dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
178
179         return 0;
180 }
181
182 static void
183 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
184 {
185         /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
186          * correspondingly stops the ioeventfds, and reset the status of
187          * the device.
188          * For modern devices, set queue desc, avail, used in PCI bar to 0,
189          * not see any more behavior in QEMU.
190          *
191          * Here we just care about what information to deliver to vhost-user
192          * or vhost-kernel. So we just close ioeventfd for now.
193          */
194         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
195
196         close(dev->callfds[vq->vq_queue_index]);
197         close(dev->kickfds[vq->vq_queue_index]);
198 }
199
200 static void
201 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
202 {
203         uint64_t buf = 1;
204         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
205
206         if (hw->cvq && (hw->cvq->vq == vq)) {
207                 virtio_user_handle_cq(dev, vq->vq_queue_index);
208                 return;
209         }
210
211         if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
212                 PMD_DRV_LOG(ERR, "failed to kick backend: %s\n",
213                             strerror(errno));
214 }
215
216 const struct virtio_pci_ops virtio_user_ops = {
217         .read_dev_cfg   = virtio_user_read_dev_config,
218         .write_dev_cfg  = virtio_user_write_dev_config,
219         .reset          = virtio_user_reset,
220         .get_status     = virtio_user_get_status,
221         .set_status     = virtio_user_set_status,
222         .get_features   = virtio_user_get_features,
223         .set_features   = virtio_user_set_features,
224         .get_isr        = virtio_user_get_isr,
225         .set_config_irq = virtio_user_set_config_irq,
226         .get_queue_num  = virtio_user_get_queue_num,
227         .setup_queue    = virtio_user_setup_queue,
228         .del_queue      = virtio_user_del_queue,
229         .notify_queue   = virtio_user_notify_queue,
230 };
231
232 static const char *valid_args[] = {
233 #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
234         VIRTIO_USER_ARG_QUEUES_NUM,
235 #define VIRTIO_USER_ARG_CQ_NUM         "cq"
236         VIRTIO_USER_ARG_CQ_NUM,
237 #define VIRTIO_USER_ARG_MAC            "mac"
238         VIRTIO_USER_ARG_MAC,
239 #define VIRTIO_USER_ARG_PATH           "path"
240         VIRTIO_USER_ARG_PATH,
241 #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
242         VIRTIO_USER_ARG_QUEUE_SIZE,
243         NULL
244 };
245
246 #define VIRTIO_USER_DEF_CQ_EN   0
247 #define VIRTIO_USER_DEF_Q_NUM   1
248 #define VIRTIO_USER_DEF_Q_SZ    256
249
250 static int
251 get_string_arg(const char *key __rte_unused,
252                const char *value, void *extra_args)
253 {
254         if (!value || !extra_args)
255                 return -EINVAL;
256
257         *(char **)extra_args = strdup(value);
258
259         return 0;
260 }
261
262 static int
263 get_integer_arg(const char *key __rte_unused,
264                 const char *value, void *extra_args)
265 {
266         if (!value || !extra_args)
267                 return -EINVAL;
268
269         *(uint64_t *)extra_args = strtoull(value, NULL, 0);
270
271         return 0;
272 }
273
274 static struct rte_eth_dev *
275 virtio_user_eth_dev_alloc(const char *name)
276 {
277         struct rte_eth_dev *eth_dev;
278         struct rte_eth_dev_data *data;
279         struct virtio_hw *hw;
280         struct virtio_user_dev *dev;
281
282         eth_dev = rte_eth_dev_allocate(name);
283         if (!eth_dev) {
284                 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
285                 return NULL;
286         }
287
288         data = eth_dev->data;
289
290         hw = rte_zmalloc(NULL, sizeof(*hw), 0);
291         if (!hw) {
292                 PMD_INIT_LOG(ERR, "malloc virtio_hw failed");
293                 rte_eth_dev_release_port(eth_dev);
294                 return NULL;
295         }
296
297         dev = rte_zmalloc(NULL, sizeof(*dev), 0);
298         if (!dev) {
299                 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
300                 rte_eth_dev_release_port(eth_dev);
301                 rte_free(hw);
302                 return NULL;
303         }
304
305         hw->port_id = data->port_id;
306         virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
307         hw->use_msix = 0;
308         hw->modern   = 0;
309         hw->use_simple_rxtx = 0;
310         hw->virtio_user_dev = dev;
311         data->dev_private = hw;
312         data->numa_node = SOCKET_ID_ANY;
313         data->kdrv = RTE_KDRV_NONE;
314         data->dev_flags = RTE_ETH_DEV_DETACHABLE;
315         eth_dev->driver = NULL;
316         return eth_dev;
317 }
318
319 static void
320 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
321 {
322         struct rte_eth_dev_data *data = eth_dev->data;
323         struct virtio_hw *hw = data->dev_private;
324
325         rte_free(hw->virtio_user_dev);
326         rte_free(hw);
327         rte_eth_dev_release_port(eth_dev);
328 }
329
330 /* Dev initialization routine. Invoked once for each virtio vdev at
331  * EAL init time, see rte_eal_dev_init().
332  * Returns 0 on success.
333  */
334 static int
335 virtio_user_pmd_probe(const char *name, const char *params)
336 {
337         struct rte_kvargs *kvlist = NULL;
338         struct rte_eth_dev *eth_dev;
339         struct virtio_hw *hw;
340         uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
341         uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
342         uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
343         char *path = NULL;
344         char *mac_addr = NULL;
345         int ret = -1;
346
347         if (!params || params[0] == '\0') {
348                 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
349                           VIRTIO_USER_ARG_QUEUE_SIZE);
350                 goto end;
351         }
352
353         kvlist = rte_kvargs_parse(params, valid_args);
354         if (!kvlist) {
355                 PMD_INIT_LOG(ERR, "error when parsing param");
356                 goto end;
357         }
358
359         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
360                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
361                                        &get_string_arg, &path) < 0) {
362                         PMD_INIT_LOG(ERR, "error to parse %s",
363                                      VIRTIO_USER_ARG_PATH);
364                         goto end;
365                 }
366         } else {
367                 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user\n",
368                           VIRTIO_USER_ARG_QUEUE_SIZE);
369                 goto end;
370         }
371
372         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
373                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
374                                        &get_string_arg, &mac_addr) < 0) {
375                         PMD_INIT_LOG(ERR, "error to parse %s",
376                                      VIRTIO_USER_ARG_MAC);
377                         goto end;
378                 }
379         }
380
381         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
382                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
383                                        &get_integer_arg, &queue_size) < 0) {
384                         PMD_INIT_LOG(ERR, "error to parse %s",
385                                      VIRTIO_USER_ARG_QUEUE_SIZE);
386                         goto end;
387                 }
388         }
389
390         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
391                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
392                                        &get_integer_arg, &queues) < 0) {
393                         PMD_INIT_LOG(ERR, "error to parse %s",
394                                      VIRTIO_USER_ARG_QUEUES_NUM);
395                         goto end;
396                 }
397         }
398
399         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
400                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
401                                        &get_integer_arg, &cq) < 0) {
402                         PMD_INIT_LOG(ERR, "error to parse %s",
403                                      VIRTIO_USER_ARG_CQ_NUM);
404                         goto end;
405                 }
406         } else if (queues > 1) {
407                 cq = 1;
408         }
409
410         if (queues > 1 && cq == 0) {
411                 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
412                 goto end;
413         }
414
415         eth_dev = virtio_user_eth_dev_alloc(name);
416         if (!eth_dev) {
417                 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
418                 goto end;
419         }
420
421         hw = eth_dev->data->dev_private;
422         if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
423                                  queue_size, mac_addr) < 0) {
424                 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
425                 virtio_user_eth_dev_free(eth_dev);
426                 goto end;
427         }
428
429         /* previously called by rte_eal_pci_probe() for physical dev */
430         if (eth_virtio_dev_init(eth_dev) < 0) {
431                 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
432                 virtio_user_eth_dev_free(eth_dev);
433                 goto end;
434         }
435         ret = 0;
436
437 end:
438         if (kvlist)
439                 rte_kvargs_free(kvlist);
440         if (path)
441                 free(path);
442         if (mac_addr)
443                 free(mac_addr);
444         return ret;
445 }
446
447 /** Called by rte_eth_dev_detach() */
448 static int
449 virtio_user_pmd_remove(const char *name)
450 {
451         struct rte_eth_dev *eth_dev;
452         struct virtio_hw *hw;
453         struct virtio_user_dev *dev;
454
455         if (!name)
456                 return -EINVAL;
457
458         PMD_DRV_LOG(INFO, "Un-Initializing %s\n", name);
459         eth_dev = rte_eth_dev_allocated(name);
460         if (!eth_dev)
461                 return -ENODEV;
462
463         /* make sure the device is stopped, queues freed */
464         rte_eth_dev_close(eth_dev->data->port_id);
465
466         hw = eth_dev->data->dev_private;
467         dev = hw->virtio_user_dev;
468         virtio_user_dev_uninit(dev);
469
470         rte_free(eth_dev->data->dev_private);
471         rte_free(eth_dev->data);
472         rte_eth_dev_release_port(eth_dev);
473
474         return 0;
475 }
476
477 static struct rte_vdev_driver virtio_user_driver = {
478         .probe = virtio_user_pmd_probe,
479         .remove = virtio_user_pmd_remove,
480 };
481
482 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
483 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
484 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
485         "path=<path> "
486         "mac=<mac addr> "
487         "cq=<int> "
488         "queue_size=<int> "
489         "queues=<int>");