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