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