New upstream version 17.11-rc3
[deb_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 #include <fcntl.h>
38 #include <sys/socket.h>
39
40 #include <rte_malloc.h>
41 #include <rte_kvargs.h>
42 #include <rte_ethdev_vdev.h>
43 #include <rte_bus_vdev.h>
44 #include <rte_alarm.h>
45
46 #include "virtio_ethdev.h"
47 #include "virtio_logs.h"
48 #include "virtio_pci.h"
49 #include "virtqueue.h"
50 #include "virtio_rxtx.h"
51 #include "virtio_user/virtio_user_dev.h"
52
53 #define virtio_user_get_dev(hw) \
54         ((struct virtio_user_dev *)(hw)->virtio_user_dev)
55
56 static void
57 virtio_user_delayed_handler(void *param)
58 {
59         struct virtio_hw *hw = (struct virtio_hw *)param;
60         struct rte_eth_dev *dev = &rte_eth_devices[hw->port_id];
61
62         rte_intr_callback_unregister(dev->intr_handle,
63                                      virtio_interrupt_handler,
64                                      dev);
65 }
66
67 static void
68 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
69                      void *dst, int length)
70 {
71         int i;
72         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
73
74         if (offset == offsetof(struct virtio_net_config, mac) &&
75             length == ETHER_ADDR_LEN) {
76                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
77                         ((uint8_t *)dst)[i] = dev->mac_addr[i];
78                 return;
79         }
80
81         if (offset == offsetof(struct virtio_net_config, status)) {
82                 char buf[128];
83
84                 if (dev->vhostfd >= 0) {
85                         int r;
86                         int flags;
87
88                         flags = fcntl(dev->vhostfd, F_GETFL);
89                         if (fcntl(dev->vhostfd, F_SETFL,
90                                         flags | O_NONBLOCK) == -1) {
91                                 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
92                                 return;
93                         }
94                         r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
95                         if (r == 0 || (r < 0 && errno != EAGAIN)) {
96                                 dev->status &= (~VIRTIO_NET_S_LINK_UP);
97                                 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
98                                             hw->port_id);
99                                 /* Only client mode is available now. Once the
100                                  * connection is broken, it can never be up
101                                  * again. Besides, this function could be called
102                                  * in the process of interrupt handling,
103                                  * callback cannot be unregistered here, set an
104                                  * alarm to do it.
105                                  */
106                                 rte_eal_alarm_set(1,
107                                                   virtio_user_delayed_handler,
108                                                   (void *)hw);
109                         } else {
110                                 dev->status |= VIRTIO_NET_S_LINK_UP;
111                         }
112                         fcntl(dev->vhostfd, F_SETFL, flags & (~O_NONBLOCK));
113                 }
114                 *(uint16_t *)dst = dev->status;
115         }
116
117         if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
118                 *(uint16_t *)dst = dev->max_queue_pairs;
119 }
120
121 static void
122 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
123                       const void *src, int length)
124 {
125         int i;
126         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
127
128         if ((offset == offsetof(struct virtio_net_config, mac)) &&
129             (length == ETHER_ADDR_LEN))
130                 for (i = 0; i < ETHER_ADDR_LEN; ++i)
131                         dev->mac_addr[i] = ((const uint8_t *)src)[i];
132         else
133                 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
134                             offset, length);
135 }
136
137 static void
138 virtio_user_reset(struct virtio_hw *hw)
139 {
140         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
141
142         if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
143                 virtio_user_stop_device(dev);
144 }
145
146 static void
147 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
148 {
149         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
150
151         if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
152                 virtio_user_start_device(dev);
153         else if (status == VIRTIO_CONFIG_STATUS_RESET)
154                 virtio_user_reset(hw);
155         dev->status = status;
156 }
157
158 static uint8_t
159 virtio_user_get_status(struct virtio_hw *hw)
160 {
161         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
162
163         return dev->status;
164 }
165
166 static uint64_t
167 virtio_user_get_features(struct virtio_hw *hw)
168 {
169         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
170
171         /* unmask feature bits defined in vhost user protocol */
172         return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
173 }
174
175 static void
176 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
177 {
178         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
179
180         dev->features = features & dev->device_features;
181 }
182
183 static uint8_t
184 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
185 {
186         /* rxq interrupts and config interrupt are separated in virtio-user,
187          * here we only report config change.
188          */
189         return VIRTIO_PCI_ISR_CONFIG;
190 }
191
192 static uint16_t
193 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
194                     uint16_t vec __rte_unused)
195 {
196         return 0;
197 }
198
199 static uint16_t
200 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
201                           struct virtqueue *vq __rte_unused,
202                           uint16_t vec)
203 {
204         /* pretend we have done that */
205         return vec;
206 }
207
208 /* This function is to get the queue size, aka, number of descs, of a specified
209  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
210  * max supported queues.
211  */
212 static uint16_t
213 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
214 {
215         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
216
217         /* Currently, each queue has same queue size */
218         return dev->queue_size;
219 }
220
221 static int
222 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
223 {
224         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
225         uint16_t queue_idx = vq->vq_queue_index;
226         uint64_t desc_addr, avail_addr, used_addr;
227
228         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
229         avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
230         used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
231                                                          ring[vq->vq_nentries]),
232                                    VIRTIO_PCI_VRING_ALIGN);
233
234         dev->vrings[queue_idx].num = vq->vq_nentries;
235         dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
236         dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
237         dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
238
239         return 0;
240 }
241
242 static void
243 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
244 {
245         /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
246          * correspondingly stops the ioeventfds, and reset the status of
247          * the device.
248          * For modern devices, set queue desc, avail, used in PCI bar to 0,
249          * not see any more behavior in QEMU.
250          *
251          * Here we just care about what information to deliver to vhost-user
252          * or vhost-kernel. So we just close ioeventfd for now.
253          */
254         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
255
256         close(dev->callfds[vq->vq_queue_index]);
257         close(dev->kickfds[vq->vq_queue_index]);
258 }
259
260 static void
261 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
262 {
263         uint64_t buf = 1;
264         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
265
266         if (hw->cvq && (hw->cvq->vq == vq)) {
267                 virtio_user_handle_cq(dev, vq->vq_queue_index);
268                 return;
269         }
270
271         if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
272                 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
273                             strerror(errno));
274 }
275
276 const struct virtio_pci_ops virtio_user_ops = {
277         .read_dev_cfg   = virtio_user_read_dev_config,
278         .write_dev_cfg  = virtio_user_write_dev_config,
279         .reset          = virtio_user_reset,
280         .get_status     = virtio_user_get_status,
281         .set_status     = virtio_user_set_status,
282         .get_features   = virtio_user_get_features,
283         .set_features   = virtio_user_set_features,
284         .get_isr        = virtio_user_get_isr,
285         .set_config_irq = virtio_user_set_config_irq,
286         .set_queue_irq  = virtio_user_set_queue_irq,
287         .get_queue_num  = virtio_user_get_queue_num,
288         .setup_queue    = virtio_user_setup_queue,
289         .del_queue      = virtio_user_del_queue,
290         .notify_queue   = virtio_user_notify_queue,
291 };
292
293 static const char *valid_args[] = {
294 #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
295         VIRTIO_USER_ARG_QUEUES_NUM,
296 #define VIRTIO_USER_ARG_CQ_NUM         "cq"
297         VIRTIO_USER_ARG_CQ_NUM,
298 #define VIRTIO_USER_ARG_MAC            "mac"
299         VIRTIO_USER_ARG_MAC,
300 #define VIRTIO_USER_ARG_PATH           "path"
301         VIRTIO_USER_ARG_PATH,
302 #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
303         VIRTIO_USER_ARG_QUEUE_SIZE,
304 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
305         VIRTIO_USER_ARG_INTERFACE_NAME,
306         NULL
307 };
308
309 #define VIRTIO_USER_DEF_CQ_EN   0
310 #define VIRTIO_USER_DEF_Q_NUM   1
311 #define VIRTIO_USER_DEF_Q_SZ    256
312
313 static int
314 get_string_arg(const char *key __rte_unused,
315                const char *value, void *extra_args)
316 {
317         if (!value || !extra_args)
318                 return -EINVAL;
319
320         *(char **)extra_args = strdup(value);
321
322         if (!*(char **)extra_args)
323                 return -ENOMEM;
324
325         return 0;
326 }
327
328 static int
329 get_integer_arg(const char *key __rte_unused,
330                 const char *value, void *extra_args)
331 {
332         if (!value || !extra_args)
333                 return -EINVAL;
334
335         *(uint64_t *)extra_args = strtoull(value, NULL, 0);
336
337         return 0;
338 }
339
340 static struct rte_vdev_driver virtio_user_driver;
341
342 static struct rte_eth_dev *
343 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
344 {
345         struct rte_eth_dev *eth_dev;
346         struct rte_eth_dev_data *data;
347         struct virtio_hw *hw;
348         struct virtio_user_dev *dev;
349
350         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
351         if (!eth_dev) {
352                 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
353                 return NULL;
354         }
355
356         data = eth_dev->data;
357         hw = eth_dev->data->dev_private;
358
359         dev = rte_zmalloc(NULL, sizeof(*dev), 0);
360         if (!dev) {
361                 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
362                 rte_eth_dev_release_port(eth_dev);
363                 rte_free(hw);
364                 return NULL;
365         }
366
367         hw->port_id = data->port_id;
368         dev->port_id = data->port_id;
369         virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
370         /*
371          * MSIX is required to enable LSC (see virtio_init_device).
372          * Here just pretend that we support msix.
373          */
374         hw->use_msix = 1;
375         hw->modern   = 0;
376         hw->use_simple_rx = 0;
377         hw->use_simple_tx = 0;
378         hw->virtio_user_dev = dev;
379         return eth_dev;
380 }
381
382 static void
383 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
384 {
385         struct rte_eth_dev_data *data = eth_dev->data;
386         struct virtio_hw *hw = data->dev_private;
387
388         rte_free(hw->virtio_user_dev);
389         rte_free(hw);
390         rte_eth_dev_release_port(eth_dev);
391 }
392
393 /* Dev initialization routine. Invoked once for each virtio vdev at
394  * EAL init time, see rte_bus_probe().
395  * Returns 0 on success.
396  */
397 static int
398 virtio_user_pmd_probe(struct rte_vdev_device *dev)
399 {
400         struct rte_kvargs *kvlist = NULL;
401         struct rte_eth_dev *eth_dev;
402         struct virtio_hw *hw;
403         uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
404         uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
405         uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
406         char *path = NULL;
407         char *ifname = NULL;
408         char *mac_addr = NULL;
409         int ret = -1;
410
411         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
412         if (!kvlist) {
413                 PMD_INIT_LOG(ERR, "error when parsing param");
414                 goto end;
415         }
416
417         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
418                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
419                                        &get_string_arg, &path) < 0) {
420                         PMD_INIT_LOG(ERR, "error to parse %s",
421                                      VIRTIO_USER_ARG_PATH);
422                         goto end;
423                 }
424         } else {
425                 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
426                           VIRTIO_USER_ARG_QUEUE_SIZE);
427                 goto end;
428         }
429
430         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
431                 if (is_vhost_user_by_type(path)) {
432                         PMD_INIT_LOG(ERR,
433                                 "arg %s applies only to vhost-kernel backend",
434                                 VIRTIO_USER_ARG_INTERFACE_NAME);
435                         goto end;
436                 }
437
438                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
439                                        &get_string_arg, &ifname) < 0) {
440                         PMD_INIT_LOG(ERR, "error to parse %s",
441                                      VIRTIO_USER_ARG_INTERFACE_NAME);
442                         goto end;
443                 }
444         }
445
446         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
447                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
448                                        &get_string_arg, &mac_addr) < 0) {
449                         PMD_INIT_LOG(ERR, "error to parse %s",
450                                      VIRTIO_USER_ARG_MAC);
451                         goto end;
452                 }
453         }
454
455         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
456                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
457                                        &get_integer_arg, &queue_size) < 0) {
458                         PMD_INIT_LOG(ERR, "error to parse %s",
459                                      VIRTIO_USER_ARG_QUEUE_SIZE);
460                         goto end;
461                 }
462         }
463
464         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
465                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
466                                        &get_integer_arg, &queues) < 0) {
467                         PMD_INIT_LOG(ERR, "error to parse %s",
468                                      VIRTIO_USER_ARG_QUEUES_NUM);
469                         goto end;
470                 }
471         }
472
473         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
474                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
475                                        &get_integer_arg, &cq) < 0) {
476                         PMD_INIT_LOG(ERR, "error to parse %s",
477                                      VIRTIO_USER_ARG_CQ_NUM);
478                         goto end;
479                 }
480         } else if (queues > 1) {
481                 cq = 1;
482         }
483
484         if (queues > 1 && cq == 0) {
485                 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
486                 goto end;
487         }
488
489         if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
490                 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
491                         VIRTIO_USER_ARG_QUEUES_NUM, queues,
492                         VIRTIO_MAX_VIRTQUEUE_PAIRS);
493                 goto end;
494         }
495
496         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
497                 eth_dev = virtio_user_eth_dev_alloc(dev);
498                 if (!eth_dev) {
499                         PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
500                         goto end;
501                 }
502
503                 hw = eth_dev->data->dev_private;
504                 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
505                                  queue_size, mac_addr, &ifname) < 0) {
506                         PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
507                         virtio_user_eth_dev_free(eth_dev);
508                         goto end;
509                 }
510         } else {
511                 eth_dev = rte_eth_dev_attach_secondary(rte_vdev_device_name(dev));
512                 if (!eth_dev)
513                         goto end;
514         }
515
516         /* previously called by rte_pci_probe() for physical dev */
517         if (eth_virtio_dev_init(eth_dev) < 0) {
518                 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
519                 virtio_user_eth_dev_free(eth_dev);
520                 goto end;
521         }
522         ret = 0;
523
524 end:
525         if (kvlist)
526                 rte_kvargs_free(kvlist);
527         if (path)
528                 free(path);
529         if (mac_addr)
530                 free(mac_addr);
531         if (ifname)
532                 free(ifname);
533         return ret;
534 }
535
536 /** Called by rte_eth_dev_detach() */
537 static int
538 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
539 {
540         const char *name;
541         struct rte_eth_dev *eth_dev;
542         struct virtio_hw *hw;
543         struct virtio_user_dev *dev;
544
545         if (!vdev)
546                 return -EINVAL;
547
548         name = rte_vdev_device_name(vdev);
549         PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
550         eth_dev = rte_eth_dev_allocated(name);
551         if (!eth_dev)
552                 return -ENODEV;
553
554         /* make sure the device is stopped, queues freed */
555         rte_eth_dev_close(eth_dev->data->port_id);
556
557         hw = eth_dev->data->dev_private;
558         dev = hw->virtio_user_dev;
559         virtio_user_dev_uninit(dev);
560
561         rte_free(eth_dev->data->dev_private);
562         rte_eth_dev_release_port(eth_dev);
563
564         return 0;
565 }
566
567 static struct rte_vdev_driver virtio_user_driver = {
568         .probe = virtio_user_pmd_probe,
569         .remove = virtio_user_pmd_remove,
570 };
571
572 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
573 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
574 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
575         "path=<path> "
576         "mac=<mac addr> "
577         "cq=<int> "
578         "queue_size=<int> "
579         "queues=<int> "
580         "iface=<string>");