LCOV - code coverage report
Current view: top level - drivers/net/virtio - virtio_user_ethdev.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1 399 0.3 %
Date: 2026-11-01 19:11:22 Functions: 1 30 3.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 234 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2016 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <errno.h>
       6                 :            : #include <stdint.h>
       7                 :            : #include <stdlib.h>
       8                 :            : #include <sys/types.h>
       9                 :            : #include <unistd.h>
      10                 :            : #include <fcntl.h>
      11                 :            : #include <linux/major.h>
      12                 :            : #include <sys/stat.h>
      13                 :            : #include <sys/sysmacros.h>
      14                 :            : #include <sys/socket.h>
      15                 :            : 
      16                 :            : #include <rte_alarm.h>
      17                 :            : #include <rte_cycles.h>
      18                 :            : #include <rte_errno.h>
      19                 :            : #include <rte_io.h>
      20                 :            : #include <rte_kvargs.h>
      21                 :            : #include <rte_malloc.h>
      22                 :            : #include <rte_spinlock.h>
      23                 :            : #include <rte_string_fns.h>
      24                 :            : #include <ethdev_vdev.h>
      25                 :            : #include <bus_vdev_driver.h>
      26                 :            : 
      27                 :            : #include "virtio_ethdev.h"
      28                 :            : #include "virtio_logs.h"
      29                 :            : #include "virtio.h"
      30                 :            : #include "virtqueue.h"
      31                 :            : #include "virtio_rxtx.h"
      32                 :            : #include "virtio_user/virtio_user_dev.h"
      33                 :            : #include "virtio_user/vhost.h"
      34                 :            : 
      35                 :            : /* Virtio-user multiprocess communication channel */
      36                 :            : #define VIRTIO_USER_MP_NAME "virtio_user_mp"
      37                 :            : 
      38                 :            : /*
      39                 :            :  * How long a secondary waits for the primary to reply with eventfds.
      40                 :            :  * The primary's MP handler does no blocking work, so a slow reply only
      41                 :            :  * means the EAL MP thread is backed up; 5s gives ample margin without
      42                 :            :  * stalling probe for too long if the primary is gone.
      43                 :            :  */
      44                 :            : #define VIRTIO_USER_MP_TIMEOUT_SEC 5
      45                 :            : 
      46                 :            : struct virtio_user_mp_param {
      47                 :            :         char port_name[RTE_DEV_NAME_MAX_LEN];
      48                 :            : };
      49                 :            : 
      50                 :            : /*
      51                 :            :  * Per-process private data, referenced by eth_dev->process_private which (unlike
      52                 :            :  * dev_private) is NOT shared between primary and secondary processes. A secondary
      53                 :            :  * stores the kick/call fds it receives from the primary here, so it never mutates
      54                 :            :  * the primary-owned shared dev->kickfds/dev->callfds arrays. callfds are kept for
      55                 :            :  * a complete per-process view of the backend fds; only kickfds are used by the
      56                 :            :  * secondary data path today.
      57                 :            :  */
      58                 :            : struct virtio_user_proc_priv {
      59                 :            :         uint32_t nr_queues;
      60                 :            :         int *kickfds;
      61                 :            :         int *callfds;
      62                 :            : };
      63                 :            : 
      64                 :            : /* Guards one-time registration of the process-wide MP action. */
      65                 :            : static rte_spinlock_t virtio_user_mp_lock = RTE_SPINLOCK_INITIALIZER;
      66                 :            : static bool virtio_user_mp_registered;
      67                 :            : 
      68                 :            : #define virtio_user_get_dev(hwp) container_of(hwp, struct virtio_user_dev, hw)
      69                 :            : 
      70                 :            : static void
      71                 :          0 : virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
      72                 :            :                      void *dst, int length)
      73                 :            : {
      74                 :            :         int i;
      75                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
      76                 :            : 
      77                 :          0 :         if (offset == offsetof(struct virtio_net_config, mac) &&
      78         [ #  # ]:          0 :             length == RTE_ETHER_ADDR_LEN) {
      79         [ #  # ]:          0 :                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
      80                 :          0 :                         ((uint8_t *)dst)[i] = dev->mac_addr[i];
      81                 :            :                 return;
      82                 :            :         }
      83                 :            : 
      84         [ #  # ]:          0 :         if (offset == offsetof(struct virtio_net_config, status)) {
      85                 :          0 :                 virtio_user_dev_update_link_state(dev);
      86                 :            : 
      87                 :          0 :                 *(uint16_t *)dst = dev->net_status;
      88                 :            :         }
      89                 :            : 
      90         [ #  # ]:          0 :         if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
      91                 :          0 :                 *(uint16_t *)dst = dev->max_queue_pairs;
      92                 :            : 
      93         [ #  # ]:          0 :         if (offset >= offsetof(struct virtio_net_config, rss_max_key_size))
      94                 :          0 :                 virtio_user_dev_get_rss_config(dev, dst, offset, length);
      95                 :            : }
      96                 :            : 
      97                 :            : static void
      98                 :          0 : virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
      99                 :            :                       const void *src, int length)
     100                 :            : {
     101                 :            :         int i;
     102                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     103                 :            : 
     104                 :          0 :         if ((offset == offsetof(struct virtio_net_config, mac)) &&
     105         [ #  # ]:          0 :             (length == RTE_ETHER_ADDR_LEN)) {
     106         [ #  # ]:          0 :                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
     107                 :          0 :                         dev->mac_addr[i] = ((const uint8_t *)src)[i];
     108                 :          0 :                 virtio_user_dev_set_mac(dev);
     109                 :          0 :                 virtio_user_dev_get_mac(dev);
     110                 :            :         } else {
     111                 :          0 :                 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
     112                 :            :                             offset, length);
     113                 :            :         }
     114                 :          0 : }
     115                 :            : 
     116                 :            : static void
     117                 :            : virtio_user_reset(struct virtio_hw *hw)
     118                 :            : {
     119                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     120                 :            : 
     121         [ #  # ]:          0 :         if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
     122                 :          0 :                 virtio_user_stop_device(dev);
     123                 :            : }
     124                 :            : 
     125                 :            : static void
     126                 :          0 : virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
     127                 :            : {
     128                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     129                 :          0 :         uint8_t old_status = dev->status;
     130                 :            : 
     131   [ #  #  #  # ]:          0 :         if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK &&
     132                 :            :                         ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK)
     133                 :          0 :                 virtio_user_dev_set_features(dev);
     134                 :            : 
     135         [ #  # ]:          0 :         if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK) {
     136         [ #  # ]:          0 :                 if (virtio_user_start_device(dev)) {
     137                 :          0 :                         virtio_user_dev_update_status(dev);
     138                 :          0 :                         return;
     139                 :            :                 }
     140         [ #  # ]:          0 :         } else if (status == VIRTIO_CONFIG_STATUS_RESET) {
     141                 :            :                 virtio_user_reset(hw);
     142                 :            :         }
     143                 :            : 
     144                 :          0 :         virtio_user_dev_set_status(dev, status);
     145                 :            : }
     146                 :            : 
     147                 :            : static uint8_t
     148                 :          0 : virtio_user_get_status(struct virtio_hw *hw)
     149                 :            : {
     150                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     151                 :            : 
     152                 :          0 :         virtio_user_dev_update_status(dev);
     153                 :            : 
     154                 :          0 :         return dev->status;
     155                 :            : }
     156                 :            : 
     157                 :            : static uint64_t
     158                 :          0 : virtio_user_get_features(struct virtio_hw *hw)
     159                 :            : {
     160                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     161                 :            : 
     162                 :            :         /* unmask feature bits defined in vhost user protocol */
     163                 :          0 :         return (dev->device_features | dev->frontend_features) &
     164                 :            :                 VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
     165                 :            : }
     166                 :            : 
     167                 :            : static void
     168                 :          0 : virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
     169                 :            : {
     170                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     171                 :            : 
     172                 :          0 :         dev->features = features & (dev->device_features | dev->frontend_features);
     173                 :          0 : }
     174                 :            : 
     175                 :            : static int
     176                 :          0 : virtio_user_features_ok(struct virtio_hw *hw __rte_unused)
     177                 :            : {
     178                 :          0 :         return 0;
     179                 :            : }
     180                 :            : 
     181                 :            : static uint8_t
     182                 :          0 : virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
     183                 :            : {
     184                 :            :         /* rxq interrupts and config interrupt are separated in virtio-user,
     185                 :            :          * here we only report config change.
     186                 :            :          */
     187                 :          0 :         return VIRTIO_ISR_CONFIG;
     188                 :            : }
     189                 :            : 
     190                 :            : static uint16_t
     191                 :          0 : virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
     192                 :            :                     uint16_t vec __rte_unused)
     193                 :            : {
     194                 :          0 :         return 0;
     195                 :            : }
     196                 :            : 
     197                 :            : static uint16_t
     198                 :          0 : virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
     199                 :            :                           struct virtqueue *vq __rte_unused,
     200                 :            :                           uint16_t vec)
     201                 :            : {
     202                 :            :         /* pretend we have done that */
     203                 :          0 :         return vec;
     204                 :            : }
     205                 :            : 
     206                 :            : /* This function is to get the queue size, aka, number of descs, of a specified
     207                 :            :  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
     208                 :            :  * max supported queues.
     209                 :            :  */
     210                 :            : static uint16_t
     211                 :          0 : virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
     212                 :            : {
     213                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     214                 :            : 
     215                 :            :         /* Currently, each queue has same queue size */
     216                 :          0 :         return dev->queue_size;
     217                 :            : }
     218                 :            : 
     219                 :            : static void
     220                 :          0 : virtio_user_setup_queue_packed(struct virtqueue *vq,
     221                 :            :                                struct virtio_user_dev *dev)
     222                 :            : {
     223                 :          0 :         uint16_t queue_idx = vq->vq_queue_index;
     224                 :            :         struct vring_packed *vring;
     225                 :            :         uint64_t desc_addr;
     226                 :            :         uint64_t avail_addr;
     227                 :            :         uint64_t used_addr;
     228                 :            :         uint16_t i;
     229                 :            : 
     230                 :          0 :         vring  = &dev->vrings.packed[queue_idx];
     231                 :          0 :         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
     232                 :          0 :         avail_addr = desc_addr + vq->vq_nentries *
     233                 :            :                 sizeof(struct vring_packed_desc);
     234                 :          0 :         used_addr = RTE_ALIGN_CEIL(avail_addr +
     235                 :            :                            sizeof(struct vring_packed_desc_event),
     236                 :            :                            VIRTIO_VRING_ALIGN);
     237                 :          0 :         vring->num = vq->vq_nentries;
     238                 :          0 :         vring->desc_iova = vq->vq_ring_mem;
     239                 :          0 :         vring->desc = (void *)(uintptr_t)desc_addr;
     240                 :          0 :         vring->driver = (void *)(uintptr_t)avail_addr;
     241                 :          0 :         vring->device = (void *)(uintptr_t)used_addr;
     242                 :          0 :         dev->packed_queues[queue_idx].avail_wrap_counter = true;
     243                 :          0 :         dev->packed_queues[queue_idx].used_wrap_counter = true;
     244                 :          0 :         dev->packed_queues[queue_idx].used_idx = 0;
     245                 :            : 
     246         [ #  # ]:          0 :         for (i = 0; i < vring->num; i++)
     247                 :          0 :                 vring->desc[i].flags = 0;
     248                 :          0 : }
     249                 :            : 
     250                 :            : static void
     251                 :            : virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
     252                 :            : {
     253                 :          0 :         uint16_t queue_idx = vq->vq_queue_index;
     254                 :            :         uint64_t desc_addr, avail_addr, used_addr;
     255                 :            : 
     256                 :          0 :         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
     257                 :          0 :         avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
     258                 :          0 :         used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
     259                 :            :                                                          ring[vq->vq_nentries]),
     260                 :            :                                    VIRTIO_VRING_ALIGN);
     261                 :            : 
     262                 :          0 :         dev->vrings.split[queue_idx].num = vq->vq_nentries;
     263                 :          0 :         dev->vrings.split[queue_idx].desc_iova = vq->vq_ring_mem;
     264                 :          0 :         dev->vrings.split[queue_idx].desc = (void *)(uintptr_t)desc_addr;
     265                 :          0 :         dev->vrings.split[queue_idx].avail = (void *)(uintptr_t)avail_addr;
     266                 :          0 :         dev->vrings.split[queue_idx].used = (void *)(uintptr_t)used_addr;
     267                 :          0 : }
     268                 :            : 
     269                 :            : static int
     270         [ #  # ]:          0 : virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
     271                 :            : {
     272                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     273                 :            : 
     274         [ #  # ]:          0 :         if (virtio_with_packed_queue(hw))
     275                 :          0 :                 virtio_user_setup_queue_packed(vq, dev);
     276                 :            :         else
     277                 :            :                 virtio_user_setup_queue_split(vq, dev);
     278                 :            : 
     279         [ #  # ]:          0 :         if (dev->notify_area)
     280                 :          0 :                 vq->notify_addr = dev->notify_area[vq->vq_queue_index];
     281                 :            : 
     282   [ #  #  #  #  :          0 :         if (dev->hw_cvq && hw->cvq && (virtnet_cq_to_vq(hw->cvq) == vq))
                   #  # ]
     283                 :          0 :                 return virtio_user_dev_create_shadow_cvq(dev, vq);
     284                 :            : 
     285                 :            :         return 0;
     286                 :            : }
     287                 :            : 
     288                 :            : static void
     289                 :          0 : virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
     290                 :            : {
     291                 :            :         /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
     292                 :            :          * correspondingly stops the ioeventfds, and reset the status of
     293                 :            :          * the device.
     294                 :            :          * For modern devices, set queue desc, avail, used in PCI bar to 0,
     295                 :            :          * not see any more behavior in QEMU.
     296                 :            :          *
     297                 :            :          * Here we just care about what information to deliver to vhost-user
     298                 :            :          * or vhost-kernel. So we just close ioeventfd for now.
     299                 :            :          */
     300                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     301                 :            : 
     302                 :          0 :         close(dev->callfds[vq->vq_queue_index]);
     303                 :          0 :         close(dev->kickfds[vq->vq_queue_index]);
     304                 :            : 
     305   [ #  #  #  # ]:          0 :         if (hw->cvq && (virtnet_cq_to_vq(hw->cvq) == vq))
     306                 :          0 :                 virtio_user_dev_destroy_shadow_cvq(dev);
     307                 :          0 : }
     308                 :            : 
     309                 :            : /*
     310                 :            :  * Return the kick fd to notify the backend for a queue in the running process.
     311                 :            :  * The secondary uses its own fds (process_private); the primary owns dev->kickfds.
     312                 :            :  */
     313                 :            : static int
     314                 :          0 : virtio_user_get_kickfd(struct virtio_hw *hw, struct virtio_user_dev *dev,
     315                 :            :                       uint16_t queue_idx)
     316                 :            : {
     317         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
     318                 :          0 :                 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
     319                 :          0 :                 struct virtio_user_proc_priv *pp = eth_dev->process_private;
     320                 :            : 
     321   [ #  #  #  # ]:          0 :                 if (pp == NULL || queue_idx >= pp->nr_queues)
     322                 :            :                         return -1;
     323                 :          0 :                 return pp->kickfds[queue_idx];
     324                 :            :         }
     325                 :            : 
     326                 :          0 :         return dev->kickfds[queue_idx];
     327                 :            : }
     328                 :            : 
     329                 :            : static void
     330                 :          0 : virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
     331                 :            : {
     332                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     333                 :          0 :         uint64_t notify_data = 1;
     334                 :            : 
     335   [ #  #  #  # ]:          0 :         if (hw->cvq && (virtnet_cq_to_vq(hw->cvq) == vq)) {
     336                 :          0 :                 virtio_user_handle_cq(dev, vq->vq_queue_index);
     337                 :            : 
     338                 :          0 :                 return;
     339                 :            :         }
     340                 :            : 
     341         [ #  # ]:          0 :         if (!dev->notify_area) {
     342                 :          0 :                 int kickfd = virtio_user_get_kickfd(hw, dev, vq->vq_queue_index);
     343                 :            : 
     344   [ #  #  #  # ]:          0 :                 if (kickfd < 0 || write(kickfd, &notify_data,
     345                 :            :                                 sizeof(notify_data)) < 0)
     346                 :          0 :                         PMD_DRV_LOG(ERR, "failed to kick backend: %s",
     347                 :            :                                     strerror(errno));
     348                 :          0 :                 return;
     349         [ #  # ]:          0 :         } else if (!virtio_with_feature(hw, VIRTIO_F_NOTIFICATION_DATA)) {
     350                 :          0 :                 rte_write16(vq->vq_queue_index, vq->notify_addr);
     351                 :          0 :                 return;
     352                 :            :         }
     353                 :            : 
     354         [ #  # ]:          0 :         if (virtio_with_packed_queue(hw)) {
     355                 :            :                 /* Bit[0:15]: vq queue index
     356                 :            :                  * Bit[16:30]: avail index
     357                 :            :                  * Bit[31]: avail wrap counter
     358                 :            :                  */
     359                 :          0 :                 notify_data = ((uint32_t)(!!(vq->vq_packed.cached_flags &
     360                 :          0 :                                 VRING_PACKED_DESC_F_AVAIL)) << 31) |
     361                 :          0 :                                 ((uint32_t)vq->vq_avail_idx << 16) |
     362                 :          0 :                                 vq->vq_queue_index;
     363                 :            :         } else {
     364                 :            :                 /* Bit[0:15]: vq queue index
     365                 :            :                  * Bit[16:31]: avail index
     366                 :            :                  */
     367                 :          0 :                 notify_data = ((uint32_t)vq->vq_avail_idx << 16) |
     368                 :          0 :                                 vq->vq_queue_index;
     369                 :            :         }
     370                 :          0 :         rte_write32(notify_data, vq->notify_addr);
     371                 :            : }
     372                 :            : 
     373                 :            : static int
     374                 :          0 : virtio_user_dev_close(struct virtio_hw *hw)
     375                 :            : {
     376                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     377                 :            : 
     378                 :          0 :         virtio_user_dev_uninit(dev);
     379                 :            : 
     380                 :          0 :         return 0;
     381                 :            : }
     382                 :            : 
     383                 :            : const struct virtio_ops virtio_user_ops = {
     384                 :            :         .read_dev_cfg   = virtio_user_read_dev_config,
     385                 :            :         .write_dev_cfg  = virtio_user_write_dev_config,
     386                 :            :         .get_status     = virtio_user_get_status,
     387                 :            :         .set_status     = virtio_user_set_status,
     388                 :            :         .get_features   = virtio_user_get_features,
     389                 :            :         .set_features   = virtio_user_set_features,
     390                 :            :         .features_ok    = virtio_user_features_ok,
     391                 :            :         .get_isr        = virtio_user_get_isr,
     392                 :            :         .set_config_irq = virtio_user_set_config_irq,
     393                 :            :         .set_queue_irq  = virtio_user_set_queue_irq,
     394                 :            :         .get_queue_num  = virtio_user_get_queue_num,
     395                 :            :         .setup_queue    = virtio_user_setup_queue,
     396                 :            :         .del_queue      = virtio_user_del_queue,
     397                 :            :         .notify_queue   = virtio_user_notify_queue,
     398                 :            :         .dev_close      = virtio_user_dev_close,
     399                 :            : };
     400                 :            : 
     401                 :            : static const char *valid_args[] = {
     402                 :            : #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
     403                 :            :         VIRTIO_USER_ARG_QUEUES_NUM,
     404                 :            : #define VIRTIO_USER_ARG_CQ_NUM         "cq"
     405                 :            :         VIRTIO_USER_ARG_CQ_NUM,
     406                 :            : #define VIRTIO_USER_ARG_MAC            "mac"
     407                 :            :         VIRTIO_USER_ARG_MAC,
     408                 :            : #define VIRTIO_USER_ARG_PATH           "path"
     409                 :            :         VIRTIO_USER_ARG_PATH,
     410                 :            : #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
     411                 :            :         VIRTIO_USER_ARG_QUEUE_SIZE,
     412                 :            : #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
     413                 :            :         VIRTIO_USER_ARG_INTERFACE_NAME,
     414                 :            : #define VIRTIO_USER_ARG_SERVER_MODE    "server"
     415                 :            :         VIRTIO_USER_ARG_SERVER_MODE,
     416                 :            : #define VIRTIO_USER_ARG_MRG_RXBUF      "mrg_rxbuf"
     417                 :            :         VIRTIO_USER_ARG_MRG_RXBUF,
     418                 :            : #define VIRTIO_USER_ARG_IN_ORDER       "in_order"
     419                 :            :         VIRTIO_USER_ARG_IN_ORDER,
     420                 :            : #define VIRTIO_USER_ARG_PACKED_VQ      "packed_vq"
     421                 :            :         VIRTIO_USER_ARG_PACKED_VQ,
     422                 :            : #define VIRTIO_USER_ARG_SPEED          "speed"
     423                 :            :         VIRTIO_USER_ARG_SPEED,
     424                 :            : #define VIRTIO_USER_ARG_VECTORIZED     "vectorized"
     425                 :            :         VIRTIO_USER_ARG_VECTORIZED,
     426                 :            :         NULL
     427                 :            : };
     428                 :            : 
     429                 :            : #define VIRTIO_USER_DEF_CQ_EN   0
     430                 :            : #define VIRTIO_USER_DEF_Q_NUM   1
     431                 :            : #define VIRTIO_USER_DEF_Q_SZ    256
     432                 :            : #define VIRTIO_USER_DEF_SERVER_MODE     0
     433                 :            : 
     434                 :            : static int
     435                 :          0 : get_string_arg(const char *key __rte_unused,
     436                 :            :                const char *value, void *extra_args)
     437                 :            : {
     438         [ #  # ]:          0 :         if (!value || !extra_args)
     439                 :            :                 return -EINVAL;
     440                 :            : 
     441                 :          0 :         *(char **)extra_args = strdup(value);
     442                 :            : 
     443         [ #  # ]:          0 :         if (!*(char **)extra_args)
     444                 :          0 :                 return -ENOMEM;
     445                 :            : 
     446                 :            :         return 0;
     447                 :            : }
     448                 :            : 
     449                 :            : static int
     450                 :          0 : get_integer_arg(const char *key __rte_unused,
     451                 :            :                 const char *value, void *extra_args)
     452                 :            : {
     453                 :            :         uint64_t integer = 0;
     454         [ #  # ]:          0 :         if (!value || !extra_args)
     455                 :            :                 return -EINVAL;
     456                 :          0 :         errno = 0;
     457                 :          0 :         integer = strtoull(value, NULL, 0);
     458                 :            :         /* extra_args keeps default value, it should be replaced
     459                 :            :          * only in case of successful parsing of the 'value' arg
     460                 :            :          */
     461         [ #  # ]:          0 :         if (errno == 0)
     462                 :          0 :                 *(uint64_t *)extra_args = integer;
     463                 :          0 :         return -errno;
     464                 :            : }
     465                 :            : 
     466                 :            : static uint32_t
     467                 :          0 : vdpa_dynamic_major_num(void)
     468                 :            : {
     469                 :            :         FILE *fp;
     470                 :          0 :         char *line = NULL;
     471                 :          0 :         size_t size = 0;
     472                 :            :         char name[11];
     473                 :            :         bool found = false;
     474                 :            :         uint32_t num;
     475                 :            : 
     476                 :          0 :         fp = fopen("/proc/devices", "r");
     477         [ #  # ]:          0 :         if (fp == NULL) {
     478                 :          0 :                 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
     479                 :            :                              strerror(errno));
     480                 :          0 :                 return UNNAMED_MAJOR;
     481                 :            :         }
     482                 :            : 
     483         [ #  # ]:          0 :         while (getline(&line, &size, fp) > 0) {
     484                 :          0 :                 char *stripped = line + strspn(line, " ");
     485         [ #  # ]:          0 :                 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
     486         [ #  # ]:          0 :                     (strncmp(name, "vhost-vdpa", 10) == 0)) {
     487                 :            :                         found = true;
     488                 :            :                         break;
     489                 :            :                 }
     490                 :            :         }
     491                 :          0 :         free(line);
     492                 :          0 :         fclose(fp);
     493         [ #  # ]:          0 :         return found ? num : UNNAMED_MAJOR;
     494                 :            : }
     495                 :            : 
     496                 :            : static enum virtio_user_backend_type
     497                 :          0 : virtio_user_backend_type(const char *path)
     498                 :            : {
     499                 :            :         struct stat sb;
     500                 :            : 
     501         [ #  # ]:          0 :         if (stat(path, &sb) == -1) {
     502         [ #  # ]:          0 :                 if (errno == ENOENT)
     503                 :            :                         return VIRTIO_USER_BACKEND_VHOST_USER;
     504                 :            : 
     505                 :          0 :                 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)", path,
     506                 :            :                              strerror(errno));
     507                 :          0 :                 return VIRTIO_USER_BACKEND_UNKNOWN;
     508                 :            :         }
     509                 :            : 
     510         [ #  # ]:          0 :         if (S_ISSOCK(sb.st_mode)) {
     511                 :            :                 return VIRTIO_USER_BACKEND_VHOST_USER;
     512         [ #  # ]:          0 :         } else if (S_ISCHR(sb.st_mode)) {
     513         [ #  # ]:          0 :                 if (major(sb.st_rdev) == MISC_MAJOR)
     514                 :            :                         return VIRTIO_USER_BACKEND_VHOST_KERNEL;
     515         [ #  # ]:          0 :                 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
     516                 :          0 :                         return VIRTIO_USER_BACKEND_VHOST_VDPA;
     517                 :            :         }
     518                 :            :         return VIRTIO_USER_BACKEND_UNKNOWN;
     519                 :            : }
     520                 :            : 
     521                 :            : static struct rte_eth_dev *
     522                 :          0 : virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
     523                 :            : {
     524                 :            :         struct rte_eth_dev *eth_dev;
     525                 :            :         struct rte_eth_dev_data *data;
     526                 :            :         struct virtio_hw *hw;
     527                 :            :         struct virtio_user_dev *dev;
     528                 :            : 
     529                 :          0 :         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*dev));
     530         [ #  # ]:          0 :         if (!eth_dev) {
     531                 :          0 :                 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
     532                 :          0 :                 return NULL;
     533                 :            :         }
     534                 :            : 
     535                 :          0 :         data = eth_dev->data;
     536                 :          0 :         dev = eth_dev->data->dev_private;
     537                 :            :         hw = &dev->hw;
     538                 :            : 
     539                 :          0 :         hw->port_id = data->port_id;
     540                 :          0 :         VIRTIO_OPS(hw) = &virtio_user_ops;
     541                 :            : 
     542                 :          0 :         hw->intr_lsc = 1;
     543                 :          0 :         hw->use_vec_rx = 0;
     544                 :          0 :         hw->use_vec_tx = 0;
     545                 :          0 :         hw->use_inorder_rx = 0;
     546                 :          0 :         hw->use_inorder_tx = 0;
     547                 :            : 
     548                 :          0 :         return eth_dev;
     549                 :            : }
     550                 :            : 
     551                 :            : static void
     552                 :            : virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
     553                 :            : {
     554                 :          0 :         rte_eth_dev_release_port(eth_dev);
     555                 :            : }
     556                 :            : 
     557                 :            : /* Close and free a secondary's per-process eventfd storage. */
     558                 :            : static void
     559                 :          0 : virtio_user_free_proc_priv(struct rte_eth_dev *eth_dev)
     560                 :            : {
     561                 :          0 :         struct virtio_user_proc_priv *pp = eth_dev->process_private;
     562                 :            :         uint32_t i;
     563                 :            : 
     564         [ #  # ]:          0 :         if (pp == NULL)
     565                 :            :                 return;
     566                 :            : 
     567         [ #  # ]:          0 :         for (i = 0; i < pp->nr_queues; i++) {
     568   [ #  #  #  # ]:          0 :                 if (pp->kickfds != NULL && pp->kickfds[i] >= 0)
     569                 :          0 :                         close(pp->kickfds[i]);
     570   [ #  #  #  # ]:          0 :                 if (pp->callfds != NULL && pp->callfds[i] >= 0)
     571                 :          0 :                         close(pp->callfds[i]);
     572                 :            :         }
     573                 :            : 
     574                 :          0 :         rte_free(pp->kickfds);
     575                 :          0 :         rte_free(pp->callfds);
     576                 :          0 :         rte_free(pp);
     577                 :          0 :         eth_dev->process_private = NULL;
     578                 :            : }
     579                 :            : 
     580                 :            : /*
     581                 :            :  * Primary-side MP handler: reply with this port's kick/call eventfds so the
     582                 :            :  * requesting secondary can talk to the vhost backend. Always sends a reply
     583                 :            :  * (num_fds == 0 on error) so the secondary fails fast instead of timing out.
     584                 :            :  */
     585                 :            : static int
     586                 :          0 : virtio_user_mp_primary_handler(const struct rte_mp_msg *msg, const void *peer)
     587                 :            : {
     588                 :            :         const struct virtio_user_mp_param *param =
     589                 :            :                 (const struct virtio_user_mp_param *)msg->param;
     590                 :            :         int eventfds[VIRTIO_USER_MAX_EVENTFDS];
     591                 :            :         struct rte_eth_dev *eth_dev;
     592                 :            :         struct virtio_user_dev *dev;
     593                 :            :         struct rte_mp_msg reply;
     594                 :            :         int num_fds;
     595                 :            :         int i;
     596                 :            : 
     597                 :            :         memset(&reply, 0, sizeof(reply));
     598                 :          0 :         strlcpy(reply.name, msg->name, sizeof(reply.name));
     599                 :          0 :         reply.len_param = 0;
     600                 :          0 :         reply.num_fds = 0;
     601                 :            : 
     602                 :          0 :         eth_dev = rte_eth_dev_get_by_name(param->port_name);
     603   [ #  #  #  # ]:          0 :         if (eth_dev == NULL || eth_dev->data->dev_private == NULL) {
     604                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to find virtio_user port: %s",
     605                 :            :                              param->port_name);
     606                 :          0 :                 return rte_mp_reply(&reply, peer);
     607                 :            :         }
     608                 :            : 
     609                 :            :         dev = eth_dev->data->dev_private;
     610                 :            : 
     611                 :            :         /* serialize against control-path changes to the fd arrays */
     612                 :          0 :         pthread_mutex_lock(&dev->mutex);
     613                 :          0 :         num_fds = virtio_user_get_eventfds_from_dev(dev, eventfds);
     614         [ #  # ]:          0 :         if (num_fds >= 0 && num_fds <= RTE_MP_MAX_FD_NUM) {
     615                 :          0 :                 reply.num_fds = num_fds;
     616         [ #  # ]:          0 :                 for (i = 0; i < num_fds; i++)
     617                 :          0 :                         reply.fds[i] = eventfds[i];
     618                 :            :         } else {
     619                 :          0 :                 PMD_INIT_LOG(ERR, "Cannot share eventfds for %s (ret=%d)",
     620                 :            :                              param->port_name, num_fds);
     621                 :            :         }
     622                 :          0 :         pthread_mutex_unlock(&dev->mutex);
     623                 :            : 
     624                 :          0 :         return rte_mp_reply(&reply, peer);
     625                 :            : }
     626                 :            : 
     627                 :            : /*
     628                 :            :  * Secondary-side: request the primary's kick/call eventfds and store them in
     629                 :            :  * this process's eth_dev->process_private. The shared dev->kickfds/dev->callfds
     630                 :            :  * arrays (owned by the primary) are never touched.
     631                 :            :  */
     632                 :            : static int
     633                 :          0 : virtio_user_sync_eventfds(struct rte_eth_dev *eth_dev, struct virtio_user_dev *dev)
     634                 :            : {
     635                 :            :         struct rte_mp_msg mp_req, *mp_rep;
     636                 :          0 :         struct rte_mp_reply mp_reply = {0};
     637                 :            :         struct virtio_user_mp_param *req_param;
     638                 :          0 :         struct timespec ts = {.tv_sec = VIRTIO_USER_MP_TIMEOUT_SEC, .tv_nsec = 0};
     639                 :            :         struct virtio_user_proc_priv *pp;
     640                 :            :         uint32_t total_queues, i;
     641                 :            :         int nr_fds, ret = 0;
     642                 :            : 
     643         [ #  # ]:          0 :         if (dev == NULL)
     644                 :            :                 return -EINVAL;
     645                 :            : 
     646         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_SECONDARY)
     647                 :            :                 return -EINVAL;
     648                 :            : 
     649                 :          0 :         total_queues = dev->max_queue_pairs * 2 + (dev->hw_cvq ? 1 : 0);
     650                 :            : 
     651                 :          0 :         pp = rte_zmalloc("virtio_user_proc_priv", sizeof(*pp), 0);
     652         [ #  # ]:          0 :         if (pp == NULL)
     653                 :            :                 return -ENOMEM;
     654                 :            : 
     655                 :          0 :         pp->kickfds = rte_calloc("virtio_user_proc_priv",
     656                 :            :                                  total_queues, sizeof(int), 0);
     657                 :          0 :         pp->callfds = rte_calloc("virtio_user_proc_priv",
     658                 :            :                                  total_queues, sizeof(int), 0);
     659   [ #  #  #  # ]:          0 :         if (pp->kickfds == NULL || pp->callfds == NULL) {
     660                 :            :                 ret = -ENOMEM;
     661                 :          0 :                 goto err_free;
     662                 :            :         }
     663         [ #  # ]:          0 :         for (i = 0; i < total_queues; i++) {
     664                 :          0 :                 pp->kickfds[i] = -1;
     665                 :          0 :                 pp->callfds[i] = -1;
     666                 :            :         }
     667                 :            : 
     668                 :            :         memset(&mp_req, 0, sizeof(mp_req));
     669                 :            :         req_param = (struct virtio_user_mp_param *)mp_req.param;
     670                 :          0 :         strlcpy(req_param->port_name, eth_dev->data->name,
     671                 :            :                 sizeof(req_param->port_name));
     672                 :            :         strlcpy(mp_req.name, VIRTIO_USER_MP_NAME, RTE_MP_MAX_NAME_LEN);
     673                 :          0 :         mp_req.len_param = sizeof(*req_param);
     674                 :          0 :         mp_req.num_fds = 0;
     675                 :            : 
     676         [ #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) < 0 ||
     677         [ #  # ]:          0 :             mp_reply.nb_received != 1) {
     678                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to request eventfds from primary");
     679                 :          0 :                 free(mp_reply.msgs);
     680                 :            :                 ret = -EIO;
     681                 :          0 :                 goto err_free;
     682                 :            :         }
     683                 :            : 
     684                 :          0 :         mp_rep = &mp_reply.msgs[0];
     685                 :          0 :         nr_fds = mp_rep->num_fds;
     686                 :            : 
     687                 :            :         /* a partially-synced device cannot work: treat any mismatch as fatal */
     688         [ #  # ]:          0 :         if (nr_fds != (int)(total_queues * VIRTIO_USER_EVENTFDS_PER_QUEUE)) {
     689                 :          0 :                 PMD_INIT_LOG(ERR, "Expected %u eventfds, received %d",
     690                 :            :                              total_queues * VIRTIO_USER_EVENTFDS_PER_QUEUE, nr_fds);
     691         [ #  # ]:          0 :                 for (i = 0; i < (uint32_t)nr_fds; i++)
     692                 :          0 :                         close(mp_rep->fds[i]);
     693                 :          0 :                 free(mp_reply.msgs);
     694                 :            :                 ret = -EPROTO;
     695                 :          0 :                 goto err_free;
     696                 :            :         }
     697                 :            : 
     698         [ #  # ]:          0 :         for (i = 0; i < total_queues; i++) {
     699                 :          0 :                 pp->kickfds[i] = mp_rep->fds[i * VIRTIO_USER_EVENTFDS_PER_QUEUE];
     700                 :          0 :                 pp->callfds[i] = mp_rep->fds[i * VIRTIO_USER_EVENTFDS_PER_QUEUE + 1];
     701                 :            :         }
     702                 :          0 :         pp->nr_queues = total_queues;
     703                 :          0 :         free(mp_reply.msgs);
     704                 :            : 
     705                 :          0 :         eth_dev->process_private = pp;
     706                 :          0 :         PMD_INIT_LOG(DEBUG, "Synced %u queue eventfds for secondary port %s",
     707                 :            :                      total_queues, eth_dev->data->name);
     708                 :          0 :         return 0;
     709                 :            : 
     710                 :          0 : err_free:
     711                 :          0 :         rte_free(pp->kickfds);
     712                 :          0 :         rte_free(pp->callfds);
     713                 :          0 :         rte_free(pp);
     714                 :          0 :         return ret;
     715                 :            : }
     716                 :            : 
     717                 :            : /* Dev initialization routine. Invoked once for each virtio vdev at
     718                 :            :  * EAL init time, see rte_bus_probe().
     719                 :            :  * Returns 0 on success.
     720                 :            :  */
     721                 :            : static int
     722                 :          0 : virtio_user_pmd_probe(struct rte_vdev_device *vdev)
     723                 :            : {
     724                 :            :         struct rte_kvargs *kvlist = NULL;
     725                 :            :         struct rte_eth_dev *eth_dev;
     726                 :            :         struct virtio_hw *hw;
     727                 :            :         struct virtio_user_dev *dev;
     728                 :            :         enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
     729                 :          0 :         uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
     730                 :          0 :         uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
     731                 :          0 :         uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
     732                 :          0 :         uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
     733                 :          0 :         uint64_t mrg_rxbuf = 1;
     734                 :          0 :         uint64_t in_order = 1;
     735                 :          0 :         uint64_t packed_vq = 0;
     736                 :          0 :         uint64_t vectorized = 0;
     737                 :          0 :         char *path = NULL;
     738                 :          0 :         char *ifname = NULL;
     739                 :          0 :         char *mac_addr = NULL;
     740                 :            :         int ret = -1;
     741                 :            : 
     742                 :            :         RTE_BUILD_BUG_ON(offsetof(struct virtio_user_dev, hw) != 0);
     743                 :            : 
     744         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
     745                 :            :                 const char *name = rte_vdev_device_name(vdev);
     746                 :          0 :                 eth_dev = rte_eth_dev_attach_secondary(name);
     747         [ #  # ]:          0 :                 if (!eth_dev) {
     748                 :          0 :                         PMD_INIT_LOG(ERR, "Failed to probe %s", name);
     749                 :          0 :                         return -1;
     750                 :            :                 }
     751                 :            : 
     752                 :          0 :                 dev = eth_dev->data->dev_private;
     753                 :            :                 hw = &dev->hw;
     754                 :          0 :                 VIRTIO_OPS(hw) = &virtio_user_ops;
     755                 :            : 
     756         [ #  # ]:          0 :                 if (eth_virtio_dev_init(eth_dev) < 0) {
     757                 :          0 :                         PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
     758                 :          0 :                         rte_eth_dev_release_port(eth_dev);
     759                 :          0 :                         return -1;
     760                 :            :                 }
     761                 :            : 
     762                 :          0 :                 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
     763                 :          0 :                 eth_dev->device = &vdev->device;
     764                 :            : 
     765                 :            :                 /* populate this process's eventfds before announcing the port */
     766                 :          0 :                 ret = virtio_user_sync_eventfds(eth_dev, dev);
     767         [ #  # ]:          0 :                 if (ret < 0) {
     768                 :          0 :                         PMD_INIT_LOG(ERR,
     769                 :            :                                      "Failed to sync eventfds in secondary: %d",
     770                 :            :                                      ret);
     771                 :          0 :                         rte_eth_dev_release_port(eth_dev);
     772                 :          0 :                         return ret;
     773                 :            :                 }
     774                 :            : 
     775                 :          0 :                 rte_eth_dev_probing_finish(eth_dev);
     776                 :          0 :                 return 0;
     777                 :            :         }
     778                 :            : 
     779                 :          0 :         kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_args);
     780         [ #  # ]:          0 :         if (!kvlist) {
     781                 :          0 :                 PMD_INIT_LOG(ERR, "error when parsing param");
     782                 :          0 :                 goto end;
     783                 :            :         }
     784                 :            : 
     785         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
     786         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
     787                 :            :                                        &get_string_arg, &path) < 0) {
     788                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     789                 :            :                                      VIRTIO_USER_ARG_PATH);
     790                 :          0 :                         goto end;
     791                 :            :                 }
     792                 :            :         } else {
     793                 :          0 :                 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
     794                 :            :                              VIRTIO_USER_ARG_PATH);
     795                 :          0 :                 goto end;
     796                 :            :         }
     797                 :            : 
     798                 :          0 :         backend_type = virtio_user_backend_type(path);
     799         [ #  # ]:          0 :         if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
     800                 :          0 :                 PMD_INIT_LOG(ERR,
     801                 :            :                              "unable to determine backend type for path %s",
     802                 :            :                         path);
     803                 :          0 :                 goto end;
     804                 :            :         }
     805                 :          0 :         PMD_INIT_LOG(INFO, "Backend type detected: %s",
     806                 :            :                      virtio_user_backend_strings[backend_type]);
     807                 :            : 
     808         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
     809         [ #  # ]:          0 :                 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
     810                 :          0 :                         PMD_INIT_LOG(ERR,
     811                 :            :                                 "arg %s applies only to vhost-kernel backend",
     812                 :            :                                 VIRTIO_USER_ARG_INTERFACE_NAME);
     813                 :          0 :                         goto end;
     814                 :            :                 }
     815                 :            : 
     816         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
     817                 :            :                                        &get_string_arg, &ifname) < 0) {
     818                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     819                 :            :                                      VIRTIO_USER_ARG_INTERFACE_NAME);
     820                 :          0 :                         goto end;
     821                 :            :                 }
     822                 :            :         }
     823                 :            : 
     824         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
     825         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
     826                 :            :                                        &get_string_arg, &mac_addr) < 0) {
     827                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     828                 :            :                                      VIRTIO_USER_ARG_MAC);
     829                 :          0 :                         goto end;
     830                 :            :                 }
     831                 :            :         }
     832                 :            : 
     833         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
     834         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
     835                 :            :                                        &get_integer_arg, &queue_size) < 0) {
     836                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     837                 :            :                                      VIRTIO_USER_ARG_QUEUE_SIZE);
     838                 :          0 :                         goto end;
     839                 :            :                 }
     840                 :            :         }
     841                 :            : 
     842         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
     843         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
     844                 :            :                                        &get_integer_arg, &queues) < 0) {
     845                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     846                 :            :                                      VIRTIO_USER_ARG_QUEUES_NUM);
     847                 :          0 :                         goto end;
     848                 :            :                 }
     849                 :            :         }
     850                 :            : 
     851         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
     852         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
     853                 :            :                                        &get_integer_arg, &server_mode) < 0) {
     854                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     855                 :            :                                      VIRTIO_USER_ARG_SERVER_MODE);
     856                 :          0 :                         goto end;
     857                 :            :                 }
     858                 :            :         }
     859                 :            : 
     860         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
     861         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
     862                 :            :                                        &get_integer_arg, &cq) < 0) {
     863                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     864                 :            :                                      VIRTIO_USER_ARG_CQ_NUM);
     865                 :          0 :                         goto end;
     866                 :            :                 }
     867                 :            :         }
     868                 :            : 
     869         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
     870         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
     871                 :            :                                        &get_integer_arg, &packed_vq) < 0) {
     872                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     873                 :            :                                      VIRTIO_USER_ARG_PACKED_VQ);
     874                 :          0 :                         goto end;
     875                 :            :                 }
     876                 :            :         }
     877                 :            : 
     878         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
     879         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
     880                 :            :                                        &get_integer_arg, &vectorized) < 0) {
     881                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     882                 :            :                                      VIRTIO_USER_ARG_VECTORIZED);
     883                 :          0 :                         goto end;
     884                 :            :                 }
     885                 :            :         }
     886                 :            : 
     887         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
     888         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
     889                 :            :                                        &get_integer_arg, &mrg_rxbuf) < 0) {
     890                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     891                 :            :                                      VIRTIO_USER_ARG_MRG_RXBUF);
     892                 :          0 :                         goto end;
     893                 :            :                 }
     894                 :            :         }
     895                 :            : 
     896         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
     897         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
     898                 :            :                                        &get_integer_arg, &in_order) < 0) {
     899                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     900                 :            :                                      VIRTIO_USER_ARG_IN_ORDER);
     901                 :          0 :                         goto end;
     902                 :            :                 }
     903                 :            :         }
     904                 :            : 
     905                 :          0 :         eth_dev = virtio_user_eth_dev_alloc(vdev);
     906         [ #  # ]:          0 :         if (!eth_dev) {
     907                 :          0 :                 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
     908                 :          0 :                 goto end;
     909                 :            :         }
     910                 :            : 
     911                 :          0 :         dev = eth_dev->data->dev_private;
     912                 :            :         hw = &dev->hw;
     913         [ #  # ]:          0 :         if (virtio_user_dev_init(dev, path, (uint16_t)queues, cq,
     914                 :            :                          queue_size, mac_addr, &ifname, server_mode,
     915                 :            :                          mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
     916                 :          0 :                 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
     917                 :            :                 virtio_user_eth_dev_free(eth_dev);
     918                 :          0 :                 goto end;
     919                 :            :         }
     920                 :            : 
     921                 :            :         /*
     922                 :            :          * Virtio-user requires using virtual addresses for the descriptors
     923                 :            :          * buffers, whatever other devices require
     924                 :            :          */
     925         [ #  # ]:          0 :         if (backend_type == VIRTIO_USER_BACKEND_VHOST_VDPA)
     926                 :            :                 /* vDPA backend requires using IOVA for the buffers
     927                 :            :                  * to make it work in IOVA as PA mode also.
     928                 :            :                  */
     929                 :          0 :                 hw->use_va = false;
     930                 :            :         else
     931                 :          0 :                 hw->use_va = true;
     932                 :            : 
     933                 :            :         /* previously called by pci probing for physical dev */
     934         [ #  # ]:          0 :         if (eth_virtio_dev_init(eth_dev) < 0) {
     935                 :          0 :                 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
     936                 :          0 :                 virtio_user_dev_uninit(dev);
     937                 :            :                 virtio_user_eth_dev_free(eth_dev);
     938                 :          0 :                 goto end;
     939                 :            :         }
     940                 :            : 
     941         [ #  # ]:          0 :         if (vectorized) {
     942         [ #  # ]:          0 :                 if (packed_vq) {
     943                 :            : #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
     944                 :          0 :                         hw->use_vec_rx = 1;
     945                 :          0 :                         hw->use_vec_tx = 1;
     946                 :            : #else
     947                 :            :                         PMD_INIT_LOG(INFO,
     948                 :            :                                 "building environment do not support packed ring vectorized");
     949                 :            : #endif
     950                 :            :                 } else {
     951                 :          0 :                         hw->use_vec_rx = 1;
     952                 :            :                 }
     953                 :            :         }
     954                 :            : 
     955                 :            :         /*
     956                 :            :          * Register the process-wide MP action once so secondaries can fetch a
     957                 :            :          * port's eventfds by name. It is intentionally left registered for the
     958                 :            :          * lifetime of the process (cleaned up at exit): unregistering per device
     959                 :            :          * cannot drain handler calls already dispatched on the EAL MP thread.
     960                 :            :          */
     961                 :            :         rte_spinlock_lock(&virtio_user_mp_lock);
     962         [ #  # ]:          0 :         if (!virtio_user_mp_registered) {
     963                 :          0 :                 ret = rte_mp_action_register(VIRTIO_USER_MP_NAME,
     964                 :            :                                              virtio_user_mp_primary_handler);
     965   [ #  #  #  # ]:          0 :                 if (ret < 0 && rte_errno != EEXIST) {
     966                 :            :                         rte_spinlock_unlock(&virtio_user_mp_lock);
     967         [ #  # ]:          0 :                         if (rte_errno == ENOTSUP) {
     968                 :          0 :                                 PMD_INIT_LOG(WARNING,
     969                 :            :                                         "MP unsupported, secondary eventfd sharing disabled");
     970                 :          0 :                                 rte_eth_dev_probing_finish(eth_dev);
     971                 :            :                                 ret = 0;
     972                 :          0 :                                 goto end;
     973                 :            :                         }
     974                 :          0 :                         PMD_INIT_LOG(ERR, "Failed to register MP handler: %s",
     975                 :            :                                      strerror(rte_errno));
     976                 :          0 :                         virtio_user_dev_uninit(dev);
     977                 :            :                         virtio_user_eth_dev_free(eth_dev);
     978                 :            :                         ret = -1;
     979                 :          0 :                         goto end;
     980                 :            :                 }
     981                 :          0 :                 virtio_user_mp_registered = true;
     982                 :            :         }
     983                 :            :         rte_spinlock_unlock(&virtio_user_mp_lock);
     984                 :            : 
     985                 :          0 :         rte_eth_dev_probing_finish(eth_dev);
     986                 :            :         ret = 0;
     987                 :            : 
     988                 :          0 : end:
     989                 :          0 :         rte_kvargs_free(kvlist);
     990                 :          0 :         free(path);
     991                 :          0 :         free(mac_addr);
     992                 :          0 :         free(ifname);
     993                 :          0 :         return ret;
     994                 :            : }
     995                 :            : 
     996                 :            : static int
     997                 :          0 : virtio_user_pmd_remove(struct rte_vdev_device *vdev)
     998                 :            : {
     999                 :            :         const char *name;
    1000                 :            :         struct rte_eth_dev *eth_dev;
    1001                 :            : 
    1002         [ #  # ]:          0 :         if (!vdev)
    1003                 :            :                 return -EINVAL;
    1004                 :            : 
    1005                 :            :         name = rte_vdev_device_name(vdev);
    1006                 :          0 :         PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
    1007                 :          0 :         eth_dev = rte_eth_dev_allocated(name);
    1008                 :            :         /* Port has already been released by close. */
    1009         [ #  # ]:          0 :         if (!eth_dev)
    1010                 :            :                 return 0;
    1011                 :            : 
    1012         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
    1013                 :          0 :                 virtio_user_free_proc_priv(eth_dev);
    1014                 :          0 :                 return rte_eth_dev_release_port(eth_dev);
    1015                 :            :         }
    1016                 :            : 
    1017                 :            :         /* make sure the device is stopped, queues freed */
    1018                 :          0 :         return rte_eth_dev_close(eth_dev->data->port_id);
    1019                 :            : }
    1020                 :            : 
    1021                 :          0 : static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
    1022                 :            :                 uint64_t iova, size_t len)
    1023                 :            : {
    1024                 :            :         const char *name;
    1025                 :            :         struct rte_eth_dev *eth_dev;
    1026                 :            :         struct virtio_user_dev *dev;
    1027                 :            : 
    1028         [ #  # ]:          0 :         if (!vdev)
    1029                 :            :                 return -EINVAL;
    1030                 :            : 
    1031                 :            :         name = rte_vdev_device_name(vdev);
    1032                 :          0 :         eth_dev = rte_eth_dev_allocated(name);
    1033                 :            :         /* Port has already been released by close. */
    1034         [ #  # ]:          0 :         if (!eth_dev)
    1035                 :            :                 return 0;
    1036                 :            : 
    1037                 :          0 :         dev = eth_dev->data->dev_private;
    1038                 :            : 
    1039         [ #  # ]:          0 :         if (dev->ops->dma_map)
    1040                 :          0 :                 return dev->ops->dma_map(dev, addr, iova, len);
    1041                 :            : 
    1042                 :            :         return 0;
    1043                 :            : }
    1044                 :            : 
    1045                 :          0 : static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
    1046                 :            :                 uint64_t iova, size_t len)
    1047                 :            : {
    1048                 :            :         const char *name;
    1049                 :            :         struct rte_eth_dev *eth_dev;
    1050                 :            :         struct virtio_user_dev *dev;
    1051                 :            : 
    1052         [ #  # ]:          0 :         if (!vdev)
    1053                 :            :                 return -EINVAL;
    1054                 :            : 
    1055                 :            :         name = rte_vdev_device_name(vdev);
    1056                 :          0 :         eth_dev = rte_eth_dev_allocated(name);
    1057                 :            :         /* Port has already been released by close. */
    1058         [ #  # ]:          0 :         if (!eth_dev)
    1059                 :            :                 return 0;
    1060                 :            : 
    1061                 :          0 :         dev = eth_dev->data->dev_private;
    1062                 :            : 
    1063         [ #  # ]:          0 :         if (dev->ops->dma_unmap)
    1064                 :          0 :                 return dev->ops->dma_unmap(dev, addr, iova, len);
    1065                 :            : 
    1066                 :            :         return 0;
    1067                 :            : }
    1068                 :            : 
    1069                 :            : static struct rte_vdev_driver virtio_user_driver = {
    1070                 :            :         .driver.alias = "virtio_user",
    1071                 :            :         .probe = virtio_user_pmd_probe,
    1072                 :            :         .remove = virtio_user_pmd_remove,
    1073                 :            :         .dma_map = virtio_user_pmd_dma_map,
    1074                 :            :         .dma_unmap = virtio_user_pmd_dma_unmap,
    1075                 :            : };
    1076                 :            : 
    1077                 :        305 : RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
    1078                 :            : RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
    1079                 :            :         "path=<path> "
    1080                 :            :         "mac=<mac addr> "
    1081                 :            :         "cq=<int> "
    1082                 :            :         "queue_size=<int> "
    1083                 :            :         "queues=<int> "
    1084                 :            :         "iface=<string> "
    1085                 :            :         "server=<0|1> "
    1086                 :            :         "mrg_rxbuf=<0|1> "
    1087                 :            :         "in_order=<0|1> "
    1088                 :            :         "packed_vq=<0|1> "
    1089                 :            :         "speed=<int> "
    1090                 :            :         "vectorized=<0|1>");

Generated by: LCOV version 1.14