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