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 279 0.4 %
Date: 2024-01-22 16:26:08 Functions: 1 26 3.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 162 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 <stdint.h>
       6                 :            : #include <stdlib.h>
       7                 :            : #include <sys/types.h>
       8                 :            : #include <unistd.h>
       9                 :            : #include <fcntl.h>
      10                 :            : #include <linux/major.h>
      11                 :            : #include <sys/stat.h>
      12                 :            : #include <sys/sysmacros.h>
      13                 :            : #include <sys/socket.h>
      14                 :            : 
      15                 :            : #include <rte_malloc.h>
      16                 :            : #include <rte_kvargs.h>
      17                 :            : #include <ethdev_vdev.h>
      18                 :            : #include <bus_vdev_driver.h>
      19                 :            : #include <rte_alarm.h>
      20                 :            : #include <rte_cycles.h>
      21                 :            : 
      22                 :            : #include "virtio_ethdev.h"
      23                 :            : #include "virtio_logs.h"
      24                 :            : #include "virtio.h"
      25                 :            : #include "virtqueue.h"
      26                 :            : #include "virtio_rxtx.h"
      27                 :            : #include "virtio_user/virtio_user_dev.h"
      28                 :            : #include "virtio_user/vhost.h"
      29                 :            : 
      30                 :            : #define virtio_user_get_dev(hwp) container_of(hwp, struct virtio_user_dev, hw)
      31                 :            : 
      32                 :            : static void
      33                 :          0 : virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
      34                 :            :                      void *dst, int length)
      35                 :            : {
      36                 :            :         int i;
      37                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
      38                 :            : 
      39                 :          0 :         if (offset == offsetof(struct virtio_net_config, mac) &&
      40         [ #  # ]:          0 :             length == RTE_ETHER_ADDR_LEN) {
      41         [ #  # ]:          0 :                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
      42                 :          0 :                         ((uint8_t *)dst)[i] = dev->mac_addr[i];
      43                 :            :                 return;
      44                 :            :         }
      45                 :            : 
      46         [ #  # ]:          0 :         if (offset == offsetof(struct virtio_net_config, status)) {
      47                 :          0 :                 virtio_user_dev_update_link_state(dev);
      48                 :            : 
      49                 :          0 :                 *(uint16_t *)dst = dev->net_status;
      50                 :            :         }
      51                 :            : 
      52         [ #  # ]:          0 :         if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
      53                 :          0 :                 *(uint16_t *)dst = dev->max_queue_pairs;
      54                 :            : }
      55                 :            : 
      56                 :            : static void
      57                 :          0 : virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
      58                 :            :                       const void *src, int length)
      59                 :            : {
      60                 :            :         int i;
      61                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
      62                 :            : 
      63                 :          0 :         if ((offset == offsetof(struct virtio_net_config, mac)) &&
      64         [ #  # ]:          0 :             (length == RTE_ETHER_ADDR_LEN)) {
      65         [ #  # ]:          0 :                 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
      66                 :          0 :                         dev->mac_addr[i] = ((const uint8_t *)src)[i];
      67                 :          0 :                 virtio_user_dev_set_mac(dev);
      68                 :          0 :                 virtio_user_dev_get_mac(dev);
      69                 :            :         } else {
      70                 :          0 :                 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
      71                 :            :                             offset, length);
      72                 :            :         }
      73                 :          0 : }
      74                 :            : 
      75                 :            : static void
      76                 :            : virtio_user_reset(struct virtio_hw *hw)
      77                 :            : {
      78                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
      79                 :            : 
      80         [ #  # ]:          0 :         if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
      81                 :          0 :                 virtio_user_stop_device(dev);
      82                 :            : }
      83                 :            : 
      84                 :            : static void
      85                 :          0 : virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
      86                 :            : {
      87                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
      88                 :          0 :         uint8_t old_status = dev->status;
      89                 :            : 
      90   [ #  #  #  # ]:          0 :         if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK &&
      91                 :            :                         ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK)
      92                 :          0 :                 virtio_user_dev_set_features(dev);
      93                 :            : 
      94         [ #  # ]:          0 :         if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK) {
      95         [ #  # ]:          0 :                 if (virtio_user_start_device(dev)) {
      96                 :          0 :                         virtio_user_dev_update_status(dev);
      97                 :          0 :                         return;
      98                 :            :                 }
      99         [ #  # ]:          0 :         } else if (status == VIRTIO_CONFIG_STATUS_RESET) {
     100                 :            :                 virtio_user_reset(hw);
     101                 :            :         }
     102                 :            : 
     103                 :          0 :         virtio_user_dev_set_status(dev, status);
     104                 :            : }
     105                 :            : 
     106                 :            : static uint8_t
     107                 :          0 : virtio_user_get_status(struct virtio_hw *hw)
     108                 :            : {
     109                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     110                 :            : 
     111                 :          0 :         virtio_user_dev_update_status(dev);
     112                 :            : 
     113                 :          0 :         return dev->status;
     114                 :            : }
     115                 :            : 
     116                 :            : static uint64_t
     117                 :          0 : virtio_user_get_features(struct virtio_hw *hw)
     118                 :            : {
     119                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     120                 :            : 
     121                 :            :         /* unmask feature bits defined in vhost user protocol */
     122                 :          0 :         return (dev->device_features | dev->frontend_features) &
     123                 :            :                 VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
     124                 :            : }
     125                 :            : 
     126                 :            : static void
     127                 :          0 : virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
     128                 :            : {
     129                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     130                 :            : 
     131                 :          0 :         dev->features = features & (dev->device_features | dev->frontend_features);
     132                 :          0 : }
     133                 :            : 
     134                 :            : static int
     135                 :          0 : virtio_user_features_ok(struct virtio_hw *hw __rte_unused)
     136                 :            : {
     137                 :          0 :         return 0;
     138                 :            : }
     139                 :            : 
     140                 :            : static uint8_t
     141                 :          0 : virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
     142                 :            : {
     143                 :            :         /* rxq interrupts and config interrupt are separated in virtio-user,
     144                 :            :          * here we only report config change.
     145                 :            :          */
     146                 :          0 :         return VIRTIO_ISR_CONFIG;
     147                 :            : }
     148                 :            : 
     149                 :            : static uint16_t
     150                 :          0 : virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
     151                 :            :                     uint16_t vec __rte_unused)
     152                 :            : {
     153                 :          0 :         return 0;
     154                 :            : }
     155                 :            : 
     156                 :            : static uint16_t
     157                 :          0 : virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
     158                 :            :                           struct virtqueue *vq __rte_unused,
     159                 :            :                           uint16_t vec)
     160                 :            : {
     161                 :            :         /* pretend we have done that */
     162                 :          0 :         return vec;
     163                 :            : }
     164                 :            : 
     165                 :            : /* This function is to get the queue size, aka, number of descs, of a specified
     166                 :            :  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
     167                 :            :  * max supported queues.
     168                 :            :  */
     169                 :            : static uint16_t
     170                 :          0 : virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
     171                 :            : {
     172                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     173                 :            : 
     174                 :            :         /* Currently, each queue has same queue size */
     175                 :          0 :         return dev->queue_size;
     176                 :            : }
     177                 :            : 
     178                 :            : static void
     179                 :          0 : virtio_user_setup_queue_packed(struct virtqueue *vq,
     180                 :            :                                struct virtio_user_dev *dev)
     181                 :            : {
     182                 :          0 :         uint16_t queue_idx = vq->vq_queue_index;
     183                 :            :         struct vring_packed *vring;
     184                 :            :         uint64_t desc_addr;
     185                 :            :         uint64_t avail_addr;
     186                 :            :         uint64_t used_addr;
     187                 :            :         uint16_t i;
     188                 :            : 
     189                 :          0 :         vring  = &dev->vrings.packed[queue_idx];
     190                 :          0 :         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
     191                 :          0 :         avail_addr = desc_addr + vq->vq_nentries *
     192                 :            :                 sizeof(struct vring_packed_desc);
     193                 :          0 :         used_addr = RTE_ALIGN_CEIL(avail_addr +
     194                 :            :                            sizeof(struct vring_packed_desc_event),
     195                 :            :                            VIRTIO_VRING_ALIGN);
     196                 :          0 :         vring->num = vq->vq_nentries;
     197                 :          0 :         vring->desc = (void *)(uintptr_t)desc_addr;
     198                 :          0 :         vring->driver = (void *)(uintptr_t)avail_addr;
     199                 :          0 :         vring->device = (void *)(uintptr_t)used_addr;
     200                 :          0 :         dev->packed_queues[queue_idx].avail_wrap_counter = true;
     201                 :          0 :         dev->packed_queues[queue_idx].used_wrap_counter = true;
     202                 :            : 
     203         [ #  # ]:          0 :         for (i = 0; i < vring->num; i++)
     204                 :          0 :                 vring->desc[i].flags = 0;
     205                 :          0 : }
     206                 :            : 
     207                 :            : static void
     208                 :            : virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
     209                 :            : {
     210                 :          0 :         uint16_t queue_idx = vq->vq_queue_index;
     211                 :            :         uint64_t desc_addr, avail_addr, used_addr;
     212                 :            : 
     213                 :          0 :         desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
     214                 :          0 :         avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
     215                 :          0 :         used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
     216                 :            :                                                          ring[vq->vq_nentries]),
     217                 :            :                                    VIRTIO_VRING_ALIGN);
     218                 :            : 
     219                 :          0 :         dev->vrings.split[queue_idx].num = vq->vq_nentries;
     220                 :          0 :         dev->vrings.split[queue_idx].desc = (void *)(uintptr_t)desc_addr;
     221                 :          0 :         dev->vrings.split[queue_idx].avail = (void *)(uintptr_t)avail_addr;
     222                 :          0 :         dev->vrings.split[queue_idx].used = (void *)(uintptr_t)used_addr;
     223                 :          0 : }
     224                 :            : 
     225                 :            : static int
     226         [ #  # ]:          0 : virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
     227                 :            : {
     228                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     229                 :            : 
     230         [ #  # ]:          0 :         if (virtio_with_packed_queue(hw))
     231                 :          0 :                 virtio_user_setup_queue_packed(vq, dev);
     232                 :            :         else
     233                 :            :                 virtio_user_setup_queue_split(vq, dev);
     234                 :            : 
     235   [ #  #  #  #  :          0 :         if (dev->hw_cvq && hw->cvq && (virtnet_cq_to_vq(hw->cvq) == vq))
                   #  # ]
     236                 :          0 :                 return virtio_user_dev_create_shadow_cvq(dev, vq);
     237                 :            : 
     238                 :            :         return 0;
     239                 :            : }
     240                 :            : 
     241                 :            : static void
     242                 :          0 : virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
     243                 :            : {
     244                 :            :         /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
     245                 :            :          * correspondingly stops the ioeventfds, and reset the status of
     246                 :            :          * the device.
     247                 :            :          * For modern devices, set queue desc, avail, used in PCI bar to 0,
     248                 :            :          * not see any more behavior in QEMU.
     249                 :            :          *
     250                 :            :          * Here we just care about what information to deliver to vhost-user
     251                 :            :          * or vhost-kernel. So we just close ioeventfd for now.
     252                 :            :          */
     253                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     254                 :            : 
     255                 :          0 :         close(dev->callfds[vq->vq_queue_index]);
     256                 :          0 :         close(dev->kickfds[vq->vq_queue_index]);
     257                 :            : 
     258   [ #  #  #  # ]:          0 :         if (hw->cvq && (virtnet_cq_to_vq(hw->cvq) == vq))
     259                 :          0 :                 virtio_user_dev_destroy_shadow_cvq(dev);
     260                 :          0 : }
     261                 :            : 
     262                 :            : static void
     263                 :          0 : virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
     264                 :            : {
     265                 :          0 :         uint64_t buf = 1;
     266                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     267                 :            : 
     268   [ #  #  #  # ]:          0 :         if (hw->cvq && (virtnet_cq_to_vq(hw->cvq) == vq)) {
     269                 :          0 :                 virtio_user_handle_cq(dev, vq->vq_queue_index);
     270                 :            : 
     271                 :          0 :                 return;
     272                 :            :         }
     273                 :            : 
     274         [ #  # ]:          0 :         if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
     275                 :          0 :                 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
     276                 :            :                             strerror(errno));
     277                 :            : }
     278                 :            : 
     279                 :            : static int
     280                 :          0 : virtio_user_dev_close(struct virtio_hw *hw)
     281                 :            : {
     282                 :            :         struct virtio_user_dev *dev = virtio_user_get_dev(hw);
     283                 :            : 
     284                 :          0 :         virtio_user_dev_uninit(dev);
     285                 :            : 
     286                 :          0 :         return 0;
     287                 :            : }
     288                 :            : 
     289                 :            : const struct virtio_ops virtio_user_ops = {
     290                 :            :         .read_dev_cfg   = virtio_user_read_dev_config,
     291                 :            :         .write_dev_cfg  = virtio_user_write_dev_config,
     292                 :            :         .get_status     = virtio_user_get_status,
     293                 :            :         .set_status     = virtio_user_set_status,
     294                 :            :         .get_features   = virtio_user_get_features,
     295                 :            :         .set_features   = virtio_user_set_features,
     296                 :            :         .features_ok    = virtio_user_features_ok,
     297                 :            :         .get_isr        = virtio_user_get_isr,
     298                 :            :         .set_config_irq = virtio_user_set_config_irq,
     299                 :            :         .set_queue_irq  = virtio_user_set_queue_irq,
     300                 :            :         .get_queue_num  = virtio_user_get_queue_num,
     301                 :            :         .setup_queue    = virtio_user_setup_queue,
     302                 :            :         .del_queue      = virtio_user_del_queue,
     303                 :            :         .notify_queue   = virtio_user_notify_queue,
     304                 :            :         .dev_close      = virtio_user_dev_close,
     305                 :            : };
     306                 :            : 
     307                 :            : static const char *valid_args[] = {
     308                 :            : #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
     309                 :            :         VIRTIO_USER_ARG_QUEUES_NUM,
     310                 :            : #define VIRTIO_USER_ARG_CQ_NUM         "cq"
     311                 :            :         VIRTIO_USER_ARG_CQ_NUM,
     312                 :            : #define VIRTIO_USER_ARG_MAC            "mac"
     313                 :            :         VIRTIO_USER_ARG_MAC,
     314                 :            : #define VIRTIO_USER_ARG_PATH           "path"
     315                 :            :         VIRTIO_USER_ARG_PATH,
     316                 :            : #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
     317                 :            :         VIRTIO_USER_ARG_QUEUE_SIZE,
     318                 :            : #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
     319                 :            :         VIRTIO_USER_ARG_INTERFACE_NAME,
     320                 :            : #define VIRTIO_USER_ARG_SERVER_MODE    "server"
     321                 :            :         VIRTIO_USER_ARG_SERVER_MODE,
     322                 :            : #define VIRTIO_USER_ARG_MRG_RXBUF      "mrg_rxbuf"
     323                 :            :         VIRTIO_USER_ARG_MRG_RXBUF,
     324                 :            : #define VIRTIO_USER_ARG_IN_ORDER       "in_order"
     325                 :            :         VIRTIO_USER_ARG_IN_ORDER,
     326                 :            : #define VIRTIO_USER_ARG_PACKED_VQ      "packed_vq"
     327                 :            :         VIRTIO_USER_ARG_PACKED_VQ,
     328                 :            : #define VIRTIO_USER_ARG_SPEED          "speed"
     329                 :            :         VIRTIO_USER_ARG_SPEED,
     330                 :            : #define VIRTIO_USER_ARG_VECTORIZED     "vectorized"
     331                 :            :         VIRTIO_USER_ARG_VECTORIZED,
     332                 :            :         NULL
     333                 :            : };
     334                 :            : 
     335                 :            : #define VIRTIO_USER_DEF_CQ_EN   0
     336                 :            : #define VIRTIO_USER_DEF_Q_NUM   1
     337                 :            : #define VIRTIO_USER_DEF_Q_SZ    256
     338                 :            : #define VIRTIO_USER_DEF_SERVER_MODE     0
     339                 :            : 
     340                 :            : static int
     341                 :          0 : get_string_arg(const char *key __rte_unused,
     342                 :            :                const char *value, void *extra_args)
     343                 :            : {
     344         [ #  # ]:          0 :         if (!value || !extra_args)
     345                 :            :                 return -EINVAL;
     346                 :            : 
     347                 :          0 :         *(char **)extra_args = strdup(value);
     348                 :            : 
     349         [ #  # ]:          0 :         if (!*(char **)extra_args)
     350                 :          0 :                 return -ENOMEM;
     351                 :            : 
     352                 :            :         return 0;
     353                 :            : }
     354                 :            : 
     355                 :            : static int
     356                 :          0 : get_integer_arg(const char *key __rte_unused,
     357                 :            :                 const char *value, void *extra_args)
     358                 :            : {
     359                 :            :         uint64_t integer = 0;
     360         [ #  # ]:          0 :         if (!value || !extra_args)
     361                 :            :                 return -EINVAL;
     362                 :          0 :         errno = 0;
     363                 :          0 :         integer = strtoull(value, NULL, 0);
     364                 :            :         /* extra_args keeps default value, it should be replaced
     365                 :            :          * only in case of successful parsing of the 'value' arg
     366                 :            :          */
     367         [ #  # ]:          0 :         if (errno == 0)
     368                 :          0 :                 *(uint64_t *)extra_args = integer;
     369                 :          0 :         return -errno;
     370                 :            : }
     371                 :            : 
     372                 :            : static uint32_t
     373                 :          0 : vdpa_dynamic_major_num(void)
     374                 :            : {
     375                 :            :         FILE *fp;
     376                 :          0 :         char *line = NULL;
     377                 :          0 :         size_t size = 0;
     378                 :            :         char name[11];
     379                 :            :         bool found = false;
     380                 :            :         uint32_t num;
     381                 :            : 
     382                 :          0 :         fp = fopen("/proc/devices", "r");
     383         [ #  # ]:          0 :         if (fp == NULL) {
     384                 :          0 :                 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
     385                 :            :                              strerror(errno));
     386                 :          0 :                 return UNNAMED_MAJOR;
     387                 :            :         }
     388                 :            : 
     389         [ #  # ]:          0 :         while (getline(&line, &size, fp) > 0) {
     390                 :          0 :                 char *stripped = line + strspn(line, " ");
     391         [ #  # ]:          0 :                 if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
     392         [ #  # ]:          0 :                     (strncmp(name, "vhost-vdpa", 10) == 0)) {
     393                 :            :                         found = true;
     394                 :            :                         break;
     395                 :            :                 }
     396                 :            :         }
     397                 :          0 :         free(line);
     398                 :          0 :         fclose(fp);
     399         [ #  # ]:          0 :         return found ? num : UNNAMED_MAJOR;
     400                 :            : }
     401                 :            : 
     402                 :            : static enum virtio_user_backend_type
     403                 :          0 : virtio_user_backend_type(const char *path)
     404                 :            : {
     405                 :            :         struct stat sb;
     406                 :            : 
     407         [ #  # ]:          0 :         if (stat(path, &sb) == -1) {
     408         [ #  # ]:          0 :                 if (errno == ENOENT)
     409                 :            :                         return VIRTIO_USER_BACKEND_VHOST_USER;
     410                 :            : 
     411                 :          0 :                 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)", path,
     412                 :            :                              strerror(errno));
     413                 :          0 :                 return VIRTIO_USER_BACKEND_UNKNOWN;
     414                 :            :         }
     415                 :            : 
     416         [ #  # ]:          0 :         if (S_ISSOCK(sb.st_mode)) {
     417                 :            :                 return VIRTIO_USER_BACKEND_VHOST_USER;
     418         [ #  # ]:          0 :         } else if (S_ISCHR(sb.st_mode)) {
     419         [ #  # ]:          0 :                 if (major(sb.st_rdev) == MISC_MAJOR)
     420                 :            :                         return VIRTIO_USER_BACKEND_VHOST_KERNEL;
     421         [ #  # ]:          0 :                 if (major(sb.st_rdev) == vdpa_dynamic_major_num())
     422                 :          0 :                         return VIRTIO_USER_BACKEND_VHOST_VDPA;
     423                 :            :         }
     424                 :            :         return VIRTIO_USER_BACKEND_UNKNOWN;
     425                 :            : }
     426                 :            : 
     427                 :            : static struct rte_eth_dev *
     428                 :          0 : virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
     429                 :            : {
     430                 :            :         struct rte_eth_dev *eth_dev;
     431                 :            :         struct rte_eth_dev_data *data;
     432                 :            :         struct virtio_hw *hw;
     433                 :            :         struct virtio_user_dev *dev;
     434                 :            : 
     435                 :          0 :         eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*dev));
     436         [ #  # ]:          0 :         if (!eth_dev) {
     437                 :          0 :                 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
     438                 :          0 :                 return NULL;
     439                 :            :         }
     440                 :            : 
     441                 :          0 :         data = eth_dev->data;
     442                 :          0 :         dev = eth_dev->data->dev_private;
     443                 :            :         hw = &dev->hw;
     444                 :            : 
     445                 :          0 :         hw->port_id = data->port_id;
     446                 :          0 :         VIRTIO_OPS(hw) = &virtio_user_ops;
     447                 :            : 
     448                 :          0 :         hw->intr_lsc = 1;
     449                 :          0 :         hw->use_vec_rx = 0;
     450                 :          0 :         hw->use_vec_tx = 0;
     451                 :          0 :         hw->use_inorder_rx = 0;
     452                 :          0 :         hw->use_inorder_tx = 0;
     453                 :            : 
     454                 :          0 :         return eth_dev;
     455                 :            : }
     456                 :            : 
     457                 :            : static void
     458                 :            : virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
     459                 :            : {
     460                 :          0 :         rte_eth_dev_release_port(eth_dev);
     461                 :            : }
     462                 :            : 
     463                 :            : /* Dev initialization routine. Invoked once for each virtio vdev at
     464                 :            :  * EAL init time, see rte_bus_probe().
     465                 :            :  * Returns 0 on success.
     466                 :            :  */
     467                 :            : static int
     468                 :          0 : virtio_user_pmd_probe(struct rte_vdev_device *vdev)
     469                 :            : {
     470                 :            :         struct rte_kvargs *kvlist = NULL;
     471                 :            :         struct rte_eth_dev *eth_dev;
     472                 :            :         struct virtio_hw *hw;
     473                 :            :         struct virtio_user_dev *dev;
     474                 :            :         enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
     475                 :          0 :         uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
     476                 :          0 :         uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
     477                 :          0 :         uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
     478                 :          0 :         uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
     479                 :          0 :         uint64_t mrg_rxbuf = 1;
     480                 :          0 :         uint64_t in_order = 1;
     481                 :          0 :         uint64_t packed_vq = 0;
     482                 :          0 :         uint64_t vectorized = 0;
     483                 :          0 :         char *path = NULL;
     484                 :          0 :         char *ifname = NULL;
     485                 :          0 :         char *mac_addr = NULL;
     486                 :            :         int ret = -1;
     487                 :            : 
     488                 :            :         RTE_BUILD_BUG_ON(offsetof(struct virtio_user_dev, hw) != 0);
     489                 :            : 
     490         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
     491                 :            :                 const char *name = rte_vdev_device_name(vdev);
     492                 :          0 :                 eth_dev = rte_eth_dev_attach_secondary(name);
     493         [ #  # ]:          0 :                 if (!eth_dev) {
     494                 :          0 :                         PMD_INIT_LOG(ERR, "Failed to probe %s", name);
     495                 :          0 :                         return -1;
     496                 :            :                 }
     497                 :            : 
     498                 :          0 :                 dev = eth_dev->data->dev_private;
     499                 :            :                 hw = &dev->hw;
     500                 :          0 :                 VIRTIO_OPS(hw) = &virtio_user_ops;
     501                 :            : 
     502         [ #  # ]:          0 :                 if (eth_virtio_dev_init(eth_dev) < 0) {
     503                 :          0 :                         PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
     504                 :          0 :                         rte_eth_dev_release_port(eth_dev);
     505                 :          0 :                         return -1;
     506                 :            :                 }
     507                 :            : 
     508                 :          0 :                 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
     509                 :          0 :                 eth_dev->device = &vdev->device;
     510                 :          0 :                 rte_eth_dev_probing_finish(eth_dev);
     511                 :          0 :                 return 0;
     512                 :            :         }
     513                 :            : 
     514                 :          0 :         kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_args);
     515         [ #  # ]:          0 :         if (!kvlist) {
     516                 :          0 :                 PMD_INIT_LOG(ERR, "error when parsing param");
     517                 :          0 :                 goto end;
     518                 :            :         }
     519                 :            : 
     520         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
     521         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
     522                 :            :                                        &get_string_arg, &path) < 0) {
     523                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     524                 :            :                                      VIRTIO_USER_ARG_PATH);
     525                 :          0 :                         goto end;
     526                 :            :                 }
     527                 :            :         } else {
     528                 :          0 :                 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
     529                 :            :                              VIRTIO_USER_ARG_PATH);
     530                 :          0 :                 goto end;
     531                 :            :         }
     532                 :            : 
     533                 :          0 :         backend_type = virtio_user_backend_type(path);
     534         [ #  # ]:          0 :         if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
     535                 :          0 :                 PMD_INIT_LOG(ERR,
     536                 :            :                              "unable to determine backend type for path %s",
     537                 :            :                         path);
     538                 :          0 :                 goto end;
     539                 :            :         }
     540                 :          0 :         PMD_INIT_LOG(INFO, "Backend type detected: %s",
     541                 :            :                      virtio_user_backend_strings[backend_type]);
     542                 :            : 
     543         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
     544         [ #  # ]:          0 :                 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
     545                 :          0 :                         PMD_INIT_LOG(ERR,
     546                 :            :                                 "arg %s applies only to vhost-kernel backend",
     547                 :            :                                 VIRTIO_USER_ARG_INTERFACE_NAME);
     548                 :          0 :                         goto end;
     549                 :            :                 }
     550                 :            : 
     551         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
     552                 :            :                                        &get_string_arg, &ifname) < 0) {
     553                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     554                 :            :                                      VIRTIO_USER_ARG_INTERFACE_NAME);
     555                 :          0 :                         goto end;
     556                 :            :                 }
     557                 :            :         }
     558                 :            : 
     559         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
     560         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
     561                 :            :                                        &get_string_arg, &mac_addr) < 0) {
     562                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     563                 :            :                                      VIRTIO_USER_ARG_MAC);
     564                 :          0 :                         goto end;
     565                 :            :                 }
     566                 :            :         }
     567                 :            : 
     568         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
     569         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
     570                 :            :                                        &get_integer_arg, &queue_size) < 0) {
     571                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     572                 :            :                                      VIRTIO_USER_ARG_QUEUE_SIZE);
     573                 :          0 :                         goto end;
     574                 :            :                 }
     575                 :            :         }
     576                 :            : 
     577         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
     578         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
     579                 :            :                                        &get_integer_arg, &queues) < 0) {
     580                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     581                 :            :                                      VIRTIO_USER_ARG_QUEUES_NUM);
     582                 :          0 :                         goto end;
     583                 :            :                 }
     584                 :            :         }
     585                 :            : 
     586         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
     587         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
     588                 :            :                                        &get_integer_arg, &server_mode) < 0) {
     589                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     590                 :            :                                      VIRTIO_USER_ARG_SERVER_MODE);
     591                 :          0 :                         goto end;
     592                 :            :                 }
     593                 :            :         }
     594                 :            : 
     595         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
     596         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
     597                 :            :                                        &get_integer_arg, &cq) < 0) {
     598                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     599                 :            :                                      VIRTIO_USER_ARG_CQ_NUM);
     600                 :          0 :                         goto end;
     601                 :            :                 }
     602                 :            :         }
     603                 :            : 
     604         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
     605         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
     606                 :            :                                        &get_integer_arg, &packed_vq) < 0) {
     607                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     608                 :            :                                      VIRTIO_USER_ARG_PACKED_VQ);
     609                 :          0 :                         goto end;
     610                 :            :                 }
     611                 :            :         }
     612                 :            : 
     613         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
     614         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
     615                 :            :                                        &get_integer_arg, &vectorized) < 0) {
     616                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     617                 :            :                                      VIRTIO_USER_ARG_VECTORIZED);
     618                 :          0 :                         goto end;
     619                 :            :                 }
     620                 :            :         }
     621                 :            : 
     622         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
     623         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
     624                 :            :                                        &get_integer_arg, &mrg_rxbuf) < 0) {
     625                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     626                 :            :                                      VIRTIO_USER_ARG_MRG_RXBUF);
     627                 :          0 :                         goto end;
     628                 :            :                 }
     629                 :            :         }
     630                 :            : 
     631         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
     632         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
     633                 :            :                                        &get_integer_arg, &in_order) < 0) {
     634                 :          0 :                         PMD_INIT_LOG(ERR, "error to parse %s",
     635                 :            :                                      VIRTIO_USER_ARG_IN_ORDER);
     636                 :          0 :                         goto end;
     637                 :            :                 }
     638                 :            :         }
     639                 :            : 
     640                 :          0 :         eth_dev = virtio_user_eth_dev_alloc(vdev);
     641         [ #  # ]:          0 :         if (!eth_dev) {
     642                 :          0 :                 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
     643                 :          0 :                 goto end;
     644                 :            :         }
     645                 :            : 
     646                 :          0 :         dev = eth_dev->data->dev_private;
     647                 :            :         hw = &dev->hw;
     648         [ #  # ]:          0 :         if (virtio_user_dev_init(dev, path, (uint16_t)queues, cq,
     649                 :            :                          queue_size, mac_addr, &ifname, server_mode,
     650                 :            :                          mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
     651                 :          0 :                 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
     652                 :            :                 virtio_user_eth_dev_free(eth_dev);
     653                 :          0 :                 goto end;
     654                 :            :         }
     655                 :            : 
     656                 :            :         /*
     657                 :            :          * Virtio-user requires using virtual addresses for the descriptors
     658                 :            :          * buffers, whatever other devices require
     659                 :            :          */
     660                 :          0 :         hw->use_va = true;
     661                 :            : 
     662                 :            :         /* previously called by pci probing for physical dev */
     663         [ #  # ]:          0 :         if (eth_virtio_dev_init(eth_dev) < 0) {
     664                 :          0 :                 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
     665                 :          0 :                 virtio_user_dev_uninit(dev);
     666                 :            :                 virtio_user_eth_dev_free(eth_dev);
     667                 :          0 :                 goto end;
     668                 :            :         }
     669                 :            : 
     670         [ #  # ]:          0 :         if (vectorized) {
     671         [ #  # ]:          0 :                 if (packed_vq) {
     672                 :            : #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
     673                 :          0 :                         hw->use_vec_rx = 1;
     674                 :          0 :                         hw->use_vec_tx = 1;
     675                 :            : #else
     676                 :            :                         PMD_INIT_LOG(INFO,
     677                 :            :                                 "building environment do not support packed ring vectorized");
     678                 :            : #endif
     679                 :            :                 } else {
     680                 :          0 :                         hw->use_vec_rx = 1;
     681                 :            :                 }
     682                 :            :         }
     683                 :            : 
     684                 :          0 :         rte_eth_dev_probing_finish(eth_dev);
     685                 :            :         ret = 0;
     686                 :            : 
     687                 :          0 : end:
     688                 :          0 :         rte_kvargs_free(kvlist);
     689                 :          0 :         free(path);
     690                 :          0 :         free(mac_addr);
     691                 :          0 :         free(ifname);
     692                 :          0 :         return ret;
     693                 :            : }
     694                 :            : 
     695                 :            : static int
     696                 :          0 : virtio_user_pmd_remove(struct rte_vdev_device *vdev)
     697                 :            : {
     698                 :            :         const char *name;
     699                 :            :         struct rte_eth_dev *eth_dev;
     700                 :            : 
     701         [ #  # ]:          0 :         if (!vdev)
     702                 :            :                 return -EINVAL;
     703                 :            : 
     704                 :            :         name = rte_vdev_device_name(vdev);
     705                 :          0 :         PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
     706                 :          0 :         eth_dev = rte_eth_dev_allocated(name);
     707                 :            :         /* Port has already been released by close. */
     708         [ #  # ]:          0 :         if (!eth_dev)
     709                 :            :                 return 0;
     710                 :            : 
     711         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
     712                 :          0 :                 return rte_eth_dev_release_port(eth_dev);
     713                 :            : 
     714                 :            :         /* make sure the device is stopped, queues freed */
     715                 :          0 :         return rte_eth_dev_close(eth_dev->data->port_id);
     716                 :            : }
     717                 :            : 
     718                 :          0 : static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
     719                 :            :                 uint64_t iova, size_t len)
     720                 :            : {
     721                 :            :         const char *name;
     722                 :            :         struct rte_eth_dev *eth_dev;
     723                 :            :         struct virtio_user_dev *dev;
     724                 :            : 
     725         [ #  # ]:          0 :         if (!vdev)
     726                 :            :                 return -EINVAL;
     727                 :            : 
     728                 :            :         name = rte_vdev_device_name(vdev);
     729                 :          0 :         eth_dev = rte_eth_dev_allocated(name);
     730                 :            :         /* Port has already been released by close. */
     731         [ #  # ]:          0 :         if (!eth_dev)
     732                 :            :                 return 0;
     733                 :            : 
     734                 :          0 :         dev = eth_dev->data->dev_private;
     735                 :            : 
     736         [ #  # ]:          0 :         if (dev->ops->dma_map)
     737                 :          0 :                 return dev->ops->dma_map(dev, addr, iova, len);
     738                 :            : 
     739                 :            :         return 0;
     740                 :            : }
     741                 :            : 
     742                 :          0 : static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
     743                 :            :                 uint64_t iova, size_t len)
     744                 :            : {
     745                 :            :         const char *name;
     746                 :            :         struct rte_eth_dev *eth_dev;
     747                 :            :         struct virtio_user_dev *dev;
     748                 :            : 
     749         [ #  # ]:          0 :         if (!vdev)
     750                 :            :                 return -EINVAL;
     751                 :            : 
     752                 :            :         name = rte_vdev_device_name(vdev);
     753                 :          0 :         eth_dev = rte_eth_dev_allocated(name);
     754                 :            :         /* Port has already been released by close. */
     755         [ #  # ]:          0 :         if (!eth_dev)
     756                 :            :                 return 0;
     757                 :            : 
     758                 :          0 :         dev = eth_dev->data->dev_private;
     759                 :            : 
     760         [ #  # ]:          0 :         if (dev->ops->dma_unmap)
     761                 :          0 :                 return dev->ops->dma_unmap(dev, addr, iova, len);
     762                 :            : 
     763                 :            :         return 0;
     764                 :            : }
     765                 :            : 
     766                 :            : static struct rte_vdev_driver virtio_user_driver = {
     767                 :            :         .probe = virtio_user_pmd_probe,
     768                 :            :         .remove = virtio_user_pmd_remove,
     769                 :            :         .dma_map = virtio_user_pmd_dma_map,
     770                 :            :         .dma_unmap = virtio_user_pmd_dma_unmap,
     771                 :            : };
     772                 :            : 
     773                 :        235 : RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
     774                 :            : RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
     775                 :            : RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
     776                 :            :         "path=<path> "
     777                 :            :         "mac=<mac addr> "
     778                 :            :         "cq=<int> "
     779                 :            :         "queue_size=<int> "
     780                 :            :         "queues=<int> "
     781                 :            :         "iface=<string> "
     782                 :            :         "server=<0|1> "
     783                 :            :         "mrg_rxbuf=<0|1> "
     784                 :            :         "in_order=<0|1> "
     785                 :            :         "packed_vq=<0|1> "
     786                 :            :         "speed=<int> "
     787                 :            :         "vectorized=<0|1>");

Generated by: LCOV version 1.14