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