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