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