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