net/virtio: introduce vhost-vDPA backend type
[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 <linux/major.h>
10 #include <sys/stat.h>
11 #include <sys/sysmacros.h>
12 #include <sys/socket.h>
13
14 #include <rte_malloc.h>
15 #include <rte_kvargs.h>
16 #include <rte_ethdev_vdev.h>
17 #include <rte_bus_vdev.h>
18 #include <rte_alarm.h>
19 #include <rte_cycles.h>
20
21 #include "virtio_ethdev.h"
22 #include "virtio_logs.h"
23 #include "virtio_pci.h"
24 #include "virtqueue.h"
25 #include "virtio_rxtx.h"
26 #include "virtio_user/virtio_user_dev.h"
27 #include "virtio_user/vhost.h"
28
29 #define virtio_user_get_dev(hw) \
30         ((struct virtio_user_dev *)(hw)->virtio_user_dev)
31
32 static void
33 virtio_user_reset_queues_packed(struct rte_eth_dev *dev)
34 {
35         struct virtio_hw *hw = dev->data->dev_private;
36         struct virtnet_rx *rxvq;
37         struct virtnet_tx *txvq;
38         uint16_t i;
39
40         /* Add lock to avoid queue contention. */
41         rte_spinlock_lock(&hw->state_lock);
42         hw->started = 0;
43
44         /*
45          * Waitting for datapath to complete before resetting queues.
46          * 1 ms should be enough for the ongoing Tx/Rx function to finish.
47          */
48         rte_delay_ms(1);
49
50         /* Vring reset for each Tx queue and Rx queue. */
51         for (i = 0; i < dev->data->nb_rx_queues; i++) {
52                 rxvq = dev->data->rx_queues[i];
53                 virtqueue_rxvq_reset_packed(rxvq->vq);
54                 virtio_dev_rx_queue_setup_finish(dev, i);
55         }
56
57         for (i = 0; i < dev->data->nb_tx_queues; i++) {
58                 txvq = dev->data->tx_queues[i];
59                 virtqueue_txvq_reset_packed(txvq->vq);
60         }
61
62         hw->started = 1;
63         rte_spinlock_unlock(&hw->state_lock);
64 }
65
66
67 static int
68 virtio_user_server_reconnect(struct virtio_user_dev *dev)
69 {
70         int ret;
71         int connectfd;
72         struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
73         struct virtio_hw *hw = eth_dev->data->dev_private;
74         uint64_t protocol_features;
75
76         connectfd = accept(dev->listenfd, NULL, NULL);
77         if (connectfd < 0)
78                 return -1;
79
80         dev->vhostfd = connectfd;
81         if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
82                                    &dev->device_features) < 0) {
83                 PMD_INIT_LOG(ERR, "get_features failed: %s",
84                              strerror(errno));
85                 return -1;
86         }
87
88         if (dev->device_features &
89                         (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) {
90                 if (dev->ops->send_request(dev,
91                                         VHOST_USER_GET_PROTOCOL_FEATURES,
92                                         &protocol_features))
93                         return -1;
94
95                 dev->protocol_features &= protocol_features;
96
97                 if (dev->ops->send_request(dev,
98                                         VHOST_USER_SET_PROTOCOL_FEATURES,
99                                         &dev->protocol_features))
100                         return -1;
101
102                 if (!(dev->protocol_features &
103                                 (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
104                         dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
105         }
106
107         dev->device_features |= dev->frontend_features;
108
109         /* umask vhost-user unsupported features */
110         dev->device_features &= ~(dev->unsupported_features);
111
112         dev->features &= dev->device_features;
113
114         /* For packed ring, resetting queues is required in reconnection. */
115         if (vtpci_packed_queue(hw) &&
116            (vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_DRIVER_OK)) {
117                 PMD_INIT_LOG(NOTICE, "Packets on the fly will be dropped"
118                                 " when packed ring reconnecting.");
119                 virtio_user_reset_queues_packed(eth_dev);
120         }
121
122         ret = virtio_user_start_device(dev);
123         if (ret < 0)
124                 return -1;
125
126         if (dev->queue_pairs > 1) {
127                 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
128                 if (ret != 0) {
129                         PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
130                         return -1;
131                 }
132         }
133         if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
134                 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
135                         PMD_DRV_LOG(ERR, "interrupt disable failed");
136                         return -1;
137                 }
138                 rte_intr_callback_unregister(eth_dev->intr_handle,
139                                              virtio_interrupt_handler,
140                                              eth_dev);
141                 eth_dev->intr_handle->fd = connectfd;
142                 rte_intr_callback_register(eth_dev->intr_handle,
143                                            virtio_interrupt_handler, eth_dev);
144
145                 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
146                         PMD_DRV_LOG(ERR, "interrupt enable failed");
147                         return -1;
148                 }
149         }
150         PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
151         return 0;
152 }
153
154 static void
155 virtio_user_delayed_handler(void *param)
156 {
157         struct virtio_hw *hw = (struct virtio_hw *)param;
158         struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
159         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
160
161         if (rte_intr_disable(eth_dev->intr_handle) < 0) {
162                 PMD_DRV_LOG(ERR, "interrupt disable failed");
163                 return;
164         }
165         rte_intr_callback_unregister(eth_dev->intr_handle,
166                                      virtio_interrupt_handler, eth_dev);
167         if (dev->is_server) {
168                 if (dev->vhostfd >= 0) {
169                         close(dev->vhostfd);
170                         dev->vhostfd = -1;
171                 }
172                 eth_dev->intr_handle->fd = dev->listenfd;
173                 rte_intr_callback_register(eth_dev->intr_handle,
174                                            virtio_interrupt_handler, eth_dev);
175                 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
176                         PMD_DRV_LOG(ERR, "interrupt enable failed");
177                         return;
178                 }
179         }
180 }
181
182 static void
183 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
184                      void *dst, int length)
185 {
186         int i;
187         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
188
189         if (offset == offsetof(struct virtio_net_config, mac) &&
190             length == RTE_ETHER_ADDR_LEN) {
191                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
192                         ((uint8_t *)dst)[i] = dev->mac_addr[i];
193                 return;
194         }
195
196         if (offset == offsetof(struct virtio_net_config, status)) {
197                 char buf[128];
198
199                 if (dev->vhostfd >= 0) {
200                         int r;
201                         int flags;
202
203                         flags = fcntl(dev->vhostfd, F_GETFL);
204                         if (fcntl(dev->vhostfd, F_SETFL,
205                                         flags | O_NONBLOCK) == -1) {
206                                 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
207                                 return;
208                         }
209                         r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
210                         if (r == 0 || (r < 0 && errno != EAGAIN)) {
211                                 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
212                                 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
213                                             hw->port_id);
214
215                                 /* This function could be called in the process
216                                  * of interrupt handling, callback cannot be
217                                  * unregistered here, set an alarm to do it.
218                                  */
219                                 rte_eal_alarm_set(1,
220                                                   virtio_user_delayed_handler,
221                                                   (void *)hw);
222                         } else {
223                                 dev->net_status |= VIRTIO_NET_S_LINK_UP;
224                         }
225                         if (fcntl(dev->vhostfd, F_SETFL,
226                                         flags & ~O_NONBLOCK) == -1) {
227                                 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
228                                 return;
229                         }
230                 } else if (dev->is_server) {
231                         dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
232                         if (virtio_user_server_reconnect(dev) >= 0)
233                                 dev->net_status |= VIRTIO_NET_S_LINK_UP;
234                 }
235
236                 *(uint16_t *)dst = dev->net_status;
237         }
238
239         if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
240                 *(uint16_t *)dst = dev->max_queue_pairs;
241 }
242
243 static void
244 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
245                       const void *src, int length)
246 {
247         int i;
248         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
249
250         if ((offset == offsetof(struct virtio_net_config, mac)) &&
251             (length == RTE_ETHER_ADDR_LEN))
252                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
253                         dev->mac_addr[i] = ((const uint8_t *)src)[i];
254         else
255                 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
256                             offset, length);
257 }
258
259 static void
260 virtio_user_reset(struct virtio_hw *hw)
261 {
262         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
263
264         if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
265                 virtio_user_stop_device(dev);
266 }
267
268 static void
269 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
270 {
271         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
272
273         if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
274                 virtio_user_start_device(dev);
275         else if (status == VIRTIO_CONFIG_STATUS_RESET)
276                 virtio_user_reset(hw);
277         dev->status = status;
278         virtio_user_send_status_update(dev, status);
279 }
280
281 static uint8_t
282 virtio_user_get_status(struct virtio_hw *hw)
283 {
284         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
285
286         virtio_user_update_status(dev);
287
288         return dev->status;
289 }
290
291 static uint64_t
292 virtio_user_get_features(struct virtio_hw *hw)
293 {
294         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
295
296         /* unmask feature bits defined in vhost user protocol */
297         return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
298 }
299
300 static void
301 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
302 {
303         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
304
305         dev->features = features & dev->device_features;
306 }
307
308 static uint8_t
309 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
310 {
311         /* rxq interrupts and config interrupt are separated in virtio-user,
312          * here we only report config change.
313          */
314         return VIRTIO_PCI_ISR_CONFIG;
315 }
316
317 static uint16_t
318 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
319                     uint16_t vec __rte_unused)
320 {
321         return 0;
322 }
323
324 static uint16_t
325 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
326                           struct virtqueue *vq __rte_unused,
327                           uint16_t vec)
328 {
329         /* pretend we have done that */
330         return vec;
331 }
332
333 /* This function is to get the queue size, aka, number of descs, of a specified
334  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
335  * max supported queues.
336  */
337 static uint16_t
338 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
339 {
340         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
341
342         /* Currently, each queue has same queue size */
343         return dev->queue_size;
344 }
345
346 static void
347 virtio_user_setup_queue_packed(struct virtqueue *vq,
348                                struct virtio_user_dev *dev)
349 {
350         uint16_t queue_idx = vq->vq_queue_index;
351         struct vring_packed *vring;
352         uint64_t desc_addr;
353         uint64_t avail_addr;
354         uint64_t used_addr;
355         uint16_t i;
356
357         vring  = &dev->packed_vrings[queue_idx];
358         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
359         avail_addr = desc_addr + vq->vq_nentries *
360                 sizeof(struct vring_packed_desc);
361         used_addr = RTE_ALIGN_CEIL(avail_addr +
362                            sizeof(struct vring_packed_desc_event),
363                            VIRTIO_PCI_VRING_ALIGN);
364         vring->num = vq->vq_nentries;
365         vring->desc = (void *)(uintptr_t)desc_addr;
366         vring->driver = (void *)(uintptr_t)avail_addr;
367         vring->device = (void *)(uintptr_t)used_addr;
368         dev->packed_queues[queue_idx].avail_wrap_counter = true;
369         dev->packed_queues[queue_idx].used_wrap_counter = true;
370
371         for (i = 0; i < vring->num; i++)
372                 vring->desc[i].flags = 0;
373 }
374
375 static void
376 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
377 {
378         uint16_t queue_idx = vq->vq_queue_index;
379         uint64_t desc_addr, avail_addr, used_addr;
380
381         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
382         avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
383         used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
384                                                          ring[vq->vq_nentries]),
385                                    VIRTIO_PCI_VRING_ALIGN);
386
387         dev->vrings[queue_idx].num = vq->vq_nentries;
388         dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
389         dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
390         dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
391 }
392
393 static int
394 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
395 {
396         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
397
398         if (vtpci_packed_queue(hw))
399                 virtio_user_setup_queue_packed(vq, dev);
400         else
401                 virtio_user_setup_queue_split(vq, dev);
402
403         return 0;
404 }
405
406 static void
407 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
408 {
409         /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
410          * correspondingly stops the ioeventfds, and reset the status of
411          * the device.
412          * For modern devices, set queue desc, avail, used in PCI bar to 0,
413          * not see any more behavior in QEMU.
414          *
415          * Here we just care about what information to deliver to vhost-user
416          * or vhost-kernel. So we just close ioeventfd for now.
417          */
418         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
419
420         close(dev->callfds[vq->vq_queue_index]);
421         close(dev->kickfds[vq->vq_queue_index]);
422 }
423
424 static void
425 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
426 {
427         uint64_t buf = 1;
428         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
429
430         if (hw->cvq && (hw->cvq->vq == vq)) {
431                 if (vtpci_packed_queue(vq->hw))
432                         virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
433                 else
434                         virtio_user_handle_cq(dev, vq->vq_queue_index);
435                 return;
436         }
437
438         if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
439                 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
440                             strerror(errno));
441 }
442
443 const struct virtio_pci_ops virtio_user_ops = {
444         .read_dev_cfg   = virtio_user_read_dev_config,
445         .write_dev_cfg  = virtio_user_write_dev_config,
446         .get_status     = virtio_user_get_status,
447         .set_status     = virtio_user_set_status,
448         .get_features   = virtio_user_get_features,
449         .set_features   = virtio_user_set_features,
450         .get_isr        = virtio_user_get_isr,
451         .set_config_irq = virtio_user_set_config_irq,
452         .set_queue_irq  = virtio_user_set_queue_irq,
453         .get_queue_num  = virtio_user_get_queue_num,
454         .setup_queue    = virtio_user_setup_queue,
455         .del_queue      = virtio_user_del_queue,
456         .notify_queue   = virtio_user_notify_queue,
457 };
458
459 static const char *valid_args[] = {
460 #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
461         VIRTIO_USER_ARG_QUEUES_NUM,
462 #define VIRTIO_USER_ARG_CQ_NUM         "cq"
463         VIRTIO_USER_ARG_CQ_NUM,
464 #define VIRTIO_USER_ARG_MAC            "mac"
465         VIRTIO_USER_ARG_MAC,
466 #define VIRTIO_USER_ARG_PATH           "path"
467         VIRTIO_USER_ARG_PATH,
468 #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
469         VIRTIO_USER_ARG_QUEUE_SIZE,
470 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
471         VIRTIO_USER_ARG_INTERFACE_NAME,
472 #define VIRTIO_USER_ARG_SERVER_MODE    "server"
473         VIRTIO_USER_ARG_SERVER_MODE,
474 #define VIRTIO_USER_ARG_MRG_RXBUF      "mrg_rxbuf"
475         VIRTIO_USER_ARG_MRG_RXBUF,
476 #define VIRTIO_USER_ARG_IN_ORDER       "in_order"
477         VIRTIO_USER_ARG_IN_ORDER,
478 #define VIRTIO_USER_ARG_PACKED_VQ      "packed_vq"
479         VIRTIO_USER_ARG_PACKED_VQ,
480 #define VIRTIO_USER_ARG_SPEED          "speed"
481         VIRTIO_USER_ARG_SPEED,
482 #define VIRTIO_USER_ARG_VECTORIZED     "vectorized"
483         VIRTIO_USER_ARG_VECTORIZED,
484         NULL
485 };
486
487 #define VIRTIO_USER_DEF_CQ_EN   0
488 #define VIRTIO_USER_DEF_Q_NUM   1
489 #define VIRTIO_USER_DEF_Q_SZ    256
490 #define VIRTIO_USER_DEF_SERVER_MODE     0
491
492 static int
493 get_string_arg(const char *key __rte_unused,
494                const char *value, void *extra_args)
495 {
496         if (!value || !extra_args)
497                 return -EINVAL;
498
499         *(char **)extra_args = strdup(value);
500
501         if (!*(char **)extra_args)
502                 return -ENOMEM;
503
504         return 0;
505 }
506
507 static int
508 get_integer_arg(const char *key __rte_unused,
509                 const char *value, void *extra_args)
510 {
511         uint64_t integer = 0;
512         if (!value || !extra_args)
513                 return -EINVAL;
514         errno = 0;
515         integer = strtoull(value, NULL, 0);
516         /* extra_args keeps default value, it should be replaced
517          * only in case of successful parsing of the 'value' arg
518          */
519         if (errno == 0)
520                 *(uint64_t *)extra_args = integer;
521         return -errno;
522 }
523
524 static uint32_t
525 vdpa_dynamic_major_num(void)
526 {
527         FILE *fp;
528         char *line = NULL;
529         size_t size;
530         char name[11];
531         bool found = false;
532         uint32_t num;
533
534         fp = fopen("/proc/devices", "r");
535         if (fp == NULL) {
536                 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
537                              strerror(errno));
538                 return UNNAMED_MAJOR;
539         }
540
541         while (getline(&line, &size, fp) > 0) {
542                 char *stripped = line + strspn(line, " ");
543                 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
544                     (strncmp(name, "vhost-vdpa", 10) == 0)) {
545                         found = true;
546                         break;
547                 }
548         }
549         fclose(fp);
550         return found ? num : UNNAMED_MAJOR;
551 }
552
553 static enum virtio_user_backend_type
554 virtio_user_backend_type(const char *path)
555 {
556         struct stat sb;
557
558         if (stat(path, &sb) == -1) {
559                 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path,
560                              strerror(errno));
561                 return VIRTIO_USER_BACKEND_UNKNOWN;
562         }
563
564         if (S_ISSOCK(sb.st_mode)) {
565                 return VIRTIO_USER_BACKEND_VHOST_USER;
566         } else if (S_ISCHR(sb.st_mode)) {
567                 if (major(sb.st_rdev) == MISC_MAJOR)
568                         return VIRTIO_USER_BACKEND_VHOST_KERNEL;
569                 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
570                         return VIRTIO_USER_BACKEND_VHOST_VDPA;
571         }
572         return VIRTIO_USER_BACKEND_UNKNOWN;
573 }
574
575 static struct rte_eth_dev *
576 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
577 {
578         struct rte_eth_dev *eth_dev;
579         struct rte_eth_dev_data *data;
580         struct virtio_hw *hw;
581         struct virtio_user_dev *dev;
582
583         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
584         if (!eth_dev) {
585                 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
586                 return NULL;
587         }
588
589         data = eth_dev->data;
590         hw = eth_dev->data->dev_private;
591
592         dev = rte_zmalloc(NULL, sizeof(*dev), 0);
593         if (!dev) {
594                 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
595                 rte_eth_dev_release_port(eth_dev);
596                 return NULL;
597         }
598
599         hw->port_id = data->port_id;
600         dev->port_id = data->port_id;
601         virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
602         /*
603          * MSIX is required to enable LSC (see virtio_init_device).
604          * Here just pretend that we support msix.
605          */
606         hw->use_msix = 1;
607         hw->modern   = 0;
608         hw->use_vec_rx = 0;
609         hw->use_vec_tx = 0;
610         hw->use_inorder_rx = 0;
611         hw->use_inorder_tx = 0;
612         hw->virtio_user_dev = dev;
613         return eth_dev;
614 }
615
616 static void
617 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
618 {
619         struct rte_eth_dev_data *data = eth_dev->data;
620         struct virtio_hw *hw = data->dev_private;
621
622         rte_free(hw->virtio_user_dev);
623         rte_eth_dev_release_port(eth_dev);
624 }
625
626 /* Dev initialization routine. Invoked once for each virtio vdev at
627  * EAL init time, see rte_bus_probe().
628  * Returns 0 on success.
629  */
630 static int
631 virtio_user_pmd_probe(struct rte_vdev_device *dev)
632 {
633         struct rte_kvargs *kvlist = NULL;
634         struct rte_eth_dev *eth_dev;
635         struct virtio_hw *hw;
636         enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
637         uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
638         uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
639         uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
640         uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
641         uint64_t mrg_rxbuf = 1;
642         uint64_t in_order = 1;
643         uint64_t packed_vq = 0;
644         uint64_t vectorized = 0;
645         char *path = NULL;
646         char *ifname = NULL;
647         char *mac_addr = NULL;
648         int ret = -1;
649
650         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
651                 const char *name = rte_vdev_device_name(dev);
652                 eth_dev = rte_eth_dev_attach_secondary(name);
653                 if (!eth_dev) {
654                         PMD_INIT_LOG(ERR, "Failed to probe %s", name);
655                         return -1;
656                 }
657
658                 if (eth_virtio_dev_init(eth_dev) < 0) {
659                         PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
660                         rte_eth_dev_release_port(eth_dev);
661                         return -1;
662                 }
663
664                 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
665                 eth_dev->device = &dev->device;
666                 rte_eth_dev_probing_finish(eth_dev);
667                 return 0;
668         }
669
670         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
671         if (!kvlist) {
672                 PMD_INIT_LOG(ERR, "error when parsing param");
673                 goto end;
674         }
675
676         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
677                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
678                                        &get_string_arg, &path) < 0) {
679                         PMD_INIT_LOG(ERR, "error to parse %s",
680                                      VIRTIO_USER_ARG_PATH);
681                         goto end;
682                 }
683         } else {
684                 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
685                              VIRTIO_USER_ARG_PATH);
686                 goto end;
687         }
688
689         backend_type = virtio_user_backend_type(path);
690         if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
691                 PMD_INIT_LOG(ERR,
692                              "unable to determine backend type for path %s",
693                         path);
694                 goto end;
695         }
696
697
698         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
699                 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
700                         PMD_INIT_LOG(ERR,
701                                 "arg %s applies only to vhost-kernel backend",
702                                 VIRTIO_USER_ARG_INTERFACE_NAME);
703                         goto end;
704                 }
705
706                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
707                                        &get_string_arg, &ifname) < 0) {
708                         PMD_INIT_LOG(ERR, "error to parse %s",
709                                      VIRTIO_USER_ARG_INTERFACE_NAME);
710                         goto end;
711                 }
712         }
713
714         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
715                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
716                                        &get_string_arg, &mac_addr) < 0) {
717                         PMD_INIT_LOG(ERR, "error to parse %s",
718                                      VIRTIO_USER_ARG_MAC);
719                         goto end;
720                 }
721         }
722
723         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
724                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
725                                        &get_integer_arg, &queue_size) < 0) {
726                         PMD_INIT_LOG(ERR, "error to parse %s",
727                                      VIRTIO_USER_ARG_QUEUE_SIZE);
728                         goto end;
729                 }
730         }
731
732         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
733                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
734                                        &get_integer_arg, &queues) < 0) {
735                         PMD_INIT_LOG(ERR, "error to parse %s",
736                                      VIRTIO_USER_ARG_QUEUES_NUM);
737                         goto end;
738                 }
739         }
740
741         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
742                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
743                                        &get_integer_arg, &server_mode) < 0) {
744                         PMD_INIT_LOG(ERR, "error to parse %s",
745                                      VIRTIO_USER_ARG_SERVER_MODE);
746                         goto end;
747                 }
748         }
749
750         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
751                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
752                                        &get_integer_arg, &cq) < 0) {
753                         PMD_INIT_LOG(ERR, "error to parse %s",
754                                      VIRTIO_USER_ARG_CQ_NUM);
755                         goto end;
756                 }
757         } else if (queues > 1) {
758                 cq = 1;
759         }
760
761         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
762                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
763                                        &get_integer_arg, &packed_vq) < 0) {
764                         PMD_INIT_LOG(ERR, "error to parse %s",
765                                      VIRTIO_USER_ARG_PACKED_VQ);
766                         goto end;
767                 }
768         }
769
770         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
771                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
772                                        &get_integer_arg, &vectorized) < 0) {
773                         PMD_INIT_LOG(ERR, "error to parse %s",
774                                      VIRTIO_USER_ARG_VECTORIZED);
775                         goto end;
776                 }
777         }
778
779         if (queues > 1 && cq == 0) {
780                 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
781                 goto end;
782         }
783
784         if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
785                 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
786                         VIRTIO_USER_ARG_QUEUES_NUM, queues,
787                         VIRTIO_MAX_VIRTQUEUE_PAIRS);
788                 goto end;
789         }
790
791         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
792                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
793                                        &get_integer_arg, &mrg_rxbuf) < 0) {
794                         PMD_INIT_LOG(ERR, "error to parse %s",
795                                      VIRTIO_USER_ARG_MRG_RXBUF);
796                         goto end;
797                 }
798         }
799
800         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
801                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
802                                        &get_integer_arg, &in_order) < 0) {
803                         PMD_INIT_LOG(ERR, "error to parse %s",
804                                      VIRTIO_USER_ARG_IN_ORDER);
805                         goto end;
806                 }
807         }
808
809         eth_dev = virtio_user_eth_dev_alloc(dev);
810         if (!eth_dev) {
811                 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
812                 goto end;
813         }
814
815         hw = eth_dev->data->dev_private;
816         if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
817                          queue_size, mac_addr, &ifname, server_mode,
818                          mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
819                 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
820                 virtio_user_eth_dev_free(eth_dev);
821                 goto end;
822         }
823
824         /* previously called by pci probing for physical dev */
825         if (eth_virtio_dev_init(eth_dev) < 0) {
826                 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
827                 virtio_user_eth_dev_free(eth_dev);
828                 goto end;
829         }
830
831         if (vectorized) {
832                 if (packed_vq) {
833 #if defined(CC_AVX512_SUPPORT)
834                         hw->use_vec_rx = 1;
835                         hw->use_vec_tx = 1;
836 #else
837                         PMD_INIT_LOG(INFO,
838                                 "building environment do not support packed ring vectorized");
839 #endif
840                 } else {
841                         hw->use_vec_rx = 1;
842                 }
843         }
844
845         rte_eth_dev_probing_finish(eth_dev);
846         ret = 0;
847
848 end:
849         if (kvlist)
850                 rte_kvargs_free(kvlist);
851         if (path)
852                 free(path);
853         if (mac_addr)
854                 free(mac_addr);
855         if (ifname)
856                 free(ifname);
857         return ret;
858 }
859
860 static int
861 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
862 {
863         const char *name;
864         struct rte_eth_dev *eth_dev;
865
866         if (!vdev)
867                 return -EINVAL;
868
869         name = rte_vdev_device_name(vdev);
870         PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
871         eth_dev = rte_eth_dev_allocated(name);
872         /* Port has already been released by close. */
873         if (!eth_dev)
874                 return 0;
875
876         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
877                 return rte_eth_dev_release_port(eth_dev);
878
879         /* make sure the device is stopped, queues freed */
880         rte_eth_dev_close(eth_dev->data->port_id);
881
882         return 0;
883 }
884
885 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
886                 uint64_t iova, size_t len)
887 {
888         const char *name;
889         struct rte_eth_dev *eth_dev;
890         struct virtio_user_dev *dev;
891         struct virtio_hw *hw;
892
893         if (!vdev)
894                 return -EINVAL;
895
896         name = rte_vdev_device_name(vdev);
897         eth_dev = rte_eth_dev_allocated(name);
898         /* Port has already been released by close. */
899         if (!eth_dev)
900                 return 0;
901
902         hw = (struct virtio_hw *)eth_dev->data->dev_private;
903         dev = hw->virtio_user_dev;
904
905         if (dev->ops->dma_map)
906                 return dev->ops->dma_map(dev, addr, iova, len);
907
908         return 0;
909 }
910
911 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
912                 uint64_t iova, size_t len)
913 {
914         const char *name;
915         struct rte_eth_dev *eth_dev;
916         struct virtio_user_dev *dev;
917         struct virtio_hw *hw;
918
919         if (!vdev)
920                 return -EINVAL;
921
922         name = rte_vdev_device_name(vdev);
923         eth_dev = rte_eth_dev_allocated(name);
924         /* Port has already been released by close. */
925         if (!eth_dev)
926                 return 0;
927
928         hw = (struct virtio_hw *)eth_dev->data->dev_private;
929         dev = hw->virtio_user_dev;
930
931         if (dev->ops->dma_unmap)
932                 return dev->ops->dma_unmap(dev, addr, iova, len);
933
934         return 0;
935 }
936
937 static struct rte_vdev_driver virtio_user_driver = {
938         .probe = virtio_user_pmd_probe,
939         .remove = virtio_user_pmd_remove,
940         .dma_map = virtio_user_pmd_dma_map,
941         .dma_unmap = virtio_user_pmd_dma_unmap,
942 };
943
944 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
945 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
946 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
947         "path=<path> "
948         "mac=<mac addr> "
949         "cq=<int> "
950         "queue_size=<int> "
951         "queues=<int> "
952         "iface=<string> "
953         "server=<0|1> "
954         "mrg_rxbuf=<0|1> "
955         "in_order=<0|1> "
956         "packed_vq=<0|1> "
957         "speed=<int> "
958         "vectorized=<0|1>");