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