LCOV - code coverage report
Current view: top level - drivers/net/vdev_netvsc - vdev_netvsc.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 5 290 1.7 %
Date: 2024-01-22 16:13:49 Functions: 3 15 20.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 218 0.9 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright 2017 6WIND S.A.
       3                 :            :  * Copyright 2017 Mellanox Technologies, Ltd
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <errno.h>
       7                 :            : #include <fcntl.h>
       8                 :            : #include <inttypes.h>
       9                 :            : #include <linux/sockios.h>
      10                 :            : #include <linux/netlink.h>
      11                 :            : #include <linux/rtnetlink.h>
      12                 :            : #include <net/if.h>
      13                 :            : #include <net/if_arp.h>
      14                 :            : #include <netinet/ip.h>
      15                 :            : #include <stdarg.h>
      16                 :            : #include <stddef.h>
      17                 :            : #include <stdlib.h>
      18                 :            : #include <stdint.h>
      19                 :            : #include <stdio.h>
      20                 :            : #include <string.h>
      21                 :            : #include <sys/ioctl.h>
      22                 :            : #include <sys/queue.h>
      23                 :            : #include <sys/socket.h>
      24                 :            : #include <unistd.h>
      25                 :            : 
      26                 :            : #include <rte_alarm.h>
      27                 :            : #include <bus_driver.h>
      28                 :            : #include <bus_vdev_driver.h>
      29                 :            : #include <rte_common.h>
      30                 :            : #include <dev_driver.h>
      31                 :            : #include <rte_errno.h>
      32                 :            : #include <rte_ethdev.h>
      33                 :            : #include <rte_ether.h>
      34                 :            : #include <rte_hypervisor.h>
      35                 :            : #include <rte_kvargs.h>
      36                 :            : #include <rte_log.h>
      37                 :            : #include <rte_string_fns.h>
      38                 :            : 
      39                 :            : #define VDEV_NETVSC_DRIVER net_vdev_netvsc
      40                 :            : #define VDEV_NETVSC_DRIVER_NAME RTE_STR(VDEV_NETVSC_DRIVER)
      41                 :            : #define VDEV_NETVSC_DRIVER_NAME_LEN 15
      42                 :            : #define VDEV_NETVSC_ARG_IFACE "iface"
      43                 :            : #define VDEV_NETVSC_ARG_MAC "mac"
      44                 :            : #define VDEV_NETVSC_ARG_FORCE "force"
      45                 :            : #define VDEV_NETVSC_ARG_IGNORE "ignore"
      46                 :            : #define VDEV_NETVSC_PROBE_MS 1000
      47                 :            : 
      48                 :            : #define NETVSC_CLASS_ID "{f8615163-df3e-46c5-913f-f2d2f965ed0e}"
      49                 :            : #define NETVSC_MAX_ROUTE_LINE_SIZE 300
      50                 :            : 
      51         [ -  + ]:        235 : RTE_LOG_REGISTER_DEFAULT(vdev_netvsc_logtype, NOTICE);
      52                 :            : 
      53                 :            : #define DRV_LOG(level, ...) \
      54                 :            :         rte_log(RTE_LOG_ ## level, \
      55                 :            :                 vdev_netvsc_logtype, \
      56                 :            :                 RTE_FMT(VDEV_NETVSC_DRIVER_NAME ": " \
      57                 :            :                         RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
      58                 :            :                 RTE_FMT_TAIL(__VA_ARGS__,)))
      59                 :            : 
      60                 :            : /** Context structure for a vdev_netvsc instance. */
      61                 :            : struct vdev_netvsc_ctx {
      62                 :            :         LIST_ENTRY(vdev_netvsc_ctx) entry; /**< Next entry in list. */
      63                 :            :         unsigned int id;                   /**< Unique ID. */
      64                 :            :         char name[64];                     /**< Unique name. */
      65                 :            :         char devname[64];                  /**< Fail-safe instance name. */
      66                 :            :         char devargs[256];                 /**< Fail-safe device arguments. */
      67                 :            :         char if_name[IF_NAMESIZE];         /**< NetVSC netdevice name. */
      68                 :            :         unsigned int if_index;             /**< NetVSC netdevice index. */
      69                 :            :         struct rte_ether_addr if_addr;     /**< NetVSC MAC address. */
      70                 :            :         int pipe[2];                       /**< Fail-safe communication pipe. */
      71                 :            :         char yield[256];                   /**< PCI sub-device arguments. */
      72                 :            : };
      73                 :            : 
      74                 :            : /** Context list is common to all driver instances. */
      75                 :            : static LIST_HEAD(, vdev_netvsc_ctx) vdev_netvsc_ctx_list =
      76                 :            :         LIST_HEAD_INITIALIZER(vdev_netvsc_ctx_list);
      77                 :            : 
      78                 :            : /** Number of entries in context list. */
      79                 :            : static unsigned int vdev_netvsc_ctx_count;
      80                 :            : 
      81                 :            : /** Number of driver instances relying on context list. */
      82                 :            : static unsigned int vdev_netvsc_ctx_inst;
      83                 :            : 
      84                 :            : /**
      85                 :            :  * Destroy a vdev_netvsc context instance.
      86                 :            :  *
      87                 :            :  * @param ctx
      88                 :            :  *   Context to destroy.
      89                 :            :  */
      90                 :            : static void
      91                 :          0 : vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx)
      92                 :            : {
      93         [ #  # ]:          0 :         if (ctx->pipe[0] != -1)
      94                 :          0 :                 close(ctx->pipe[0]);
      95         [ #  # ]:          0 :         if (ctx->pipe[1] != -1)
      96                 :          0 :                 close(ctx->pipe[1]);
      97                 :          0 :         free(ctx);
      98                 :          0 : }
      99                 :            : 
     100                 :            : /**
     101                 :            :  * Determine if a network interface is NetVSC.
     102                 :            :  *
     103                 :            :  * @param[in] iface
     104                 :            :  *   Pointer to netdevice description structure (name and index).
     105                 :            :  *
     106                 :            :  * @return
     107                 :            :  *   A nonzero value when interface is detected as NetVSC. In case of error,
     108                 :            :  *   rte_errno is updated and 0 returned.
     109                 :            :  */
     110                 :            : static int
     111                 :          0 : vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface)
     112                 :            : {
     113                 :            :         static const char temp[] = "/sys/class/net/%s/device/class_id";
     114                 :            :         char path[sizeof(temp) + IF_NAMESIZE];
     115                 :            :         FILE *f;
     116                 :            :         int ret;
     117                 :          0 :         int len = 0;
     118                 :            : 
     119         [ #  # ]:          0 :         ret = snprintf(path, sizeof(path), temp, iface->if_name);
     120         [ #  # ]:          0 :         if (ret == -1 || (size_t)ret >= sizeof(path)) {
     121                 :          0 :                 rte_errno = ENOBUFS;
     122                 :          0 :                 return 0;
     123                 :            :         }
     124                 :          0 :         f = fopen(path, "r");
     125         [ #  # ]:          0 :         if (!f) {
     126                 :          0 :                 rte_errno = errno;
     127                 :          0 :                 return 0;
     128                 :            :         }
     129                 :          0 :         ret = fscanf(f, NETVSC_CLASS_ID "%n", &len);
     130         [ #  # ]:          0 :         if (ret == EOF)
     131                 :          0 :                 rte_errno = errno;
     132                 :          0 :         ret = len == (int)strlen(NETVSC_CLASS_ID);
     133                 :          0 :         fclose(f);
     134                 :          0 :         return ret;
     135                 :            : }
     136                 :            : 
     137                 :            : /**
     138                 :            :  * Iterate over system network interfaces.
     139                 :            :  *
     140                 :            :  * This function runs a given callback function for each netdevice found on
     141                 :            :  * the system.
     142                 :            :  *
     143                 :            :  * @param func
     144                 :            :  *   Callback function pointer. List traversal is aborted when this function
     145                 :            :  *   returns a nonzero value.
     146                 :            :  * @param is_netvsc
     147                 :            :  *   Indicates the device type to iterate - netvsc or non-netvsc.
     148                 :            :  * @param ...
     149                 :            :  *   Variable parameter list passed as @p va_list to @p func.
     150                 :            :  *
     151                 :            :  * @return
     152                 :            :  *   0 when the entire list is traversed successfully, a negative error code
     153                 :            :  *   in case or failure, or the nonzero value returned by @p func when list
     154                 :            :  *   traversal is aborted.
     155                 :            :  */
     156                 :            : static int
     157                 :          0 : vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface,
     158                 :            :                                       const struct rte_ether_addr *eth_addr,
     159                 :            :                                       va_list ap), int is_netvsc, ...)
     160                 :            : {
     161                 :          0 :         struct if_nameindex *iface = if_nameindex();
     162                 :          0 :         int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
     163                 :            :         unsigned int i;
     164                 :            :         int ret = 0;
     165                 :            : 
     166         [ #  # ]:          0 :         if (!iface) {
     167                 :            :                 ret = -ENOBUFS;
     168                 :          0 :                 DRV_LOG(ERR, "cannot retrieve system network interfaces");
     169                 :          0 :                 goto error;
     170                 :            :         }
     171         [ #  # ]:          0 :         if (s == -1) {
     172                 :          0 :                 ret = -errno;
     173                 :          0 :                 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
     174                 :          0 :                 goto error;
     175                 :            :         }
     176         [ #  # ]:          0 :         for (i = 0; iface[i].if_name; ++i) {
     177                 :            :                 int is_netvsc_ret;
     178                 :            :                 struct ifreq req;
     179                 :            :                 struct rte_ether_addr eth_addr;
     180                 :            :                 va_list ap;
     181                 :            : 
     182                 :          0 :                 is_netvsc_ret = vdev_netvsc_iface_is_netvsc(&iface[i]) ? 1 : 0;
     183         [ #  # ]:          0 :                 if (is_netvsc ^ is_netvsc_ret)
     184                 :          0 :                         continue;
     185                 :          0 :                 strlcpy(req.ifr_name, iface[i].if_name, sizeof(req.ifr_name));
     186         [ #  # ]:          0 :                 if (ioctl(s, SIOCGIFHWADDR, &req) == -1) {
     187                 :          0 :                         DRV_LOG(WARNING, "cannot retrieve information about"
     188                 :            :                                          " interface \"%s\": %s",
     189                 :            :                                          req.ifr_name, rte_strerror(errno));
     190                 :          0 :                         continue;
     191                 :            :                 }
     192         [ #  # ]:          0 :                 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER)
     193                 :          0 :                         continue;
     194                 :            :                 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
     195                 :            :                        RTE_DIM(eth_addr.addr_bytes));
     196                 :          0 :                 va_start(ap, is_netvsc);
     197                 :          0 :                 ret = func(&iface[i], &eth_addr, ap);
     198                 :          0 :                 va_end(ap);
     199         [ #  # ]:          0 :                 if (ret)
     200                 :            :                         break;
     201                 :            :         }
     202                 :          0 : error:
     203         [ #  # ]:          0 :         if (s != -1)
     204                 :          0 :                 close(s);
     205         [ #  # ]:          0 :         if (iface)
     206                 :          0 :                 if_freenameindex(iface);
     207                 :          0 :         return ret;
     208                 :            : }
     209                 :            : 
     210                 :            : /**
     211                 :            :  * Determine if a network interface has a route.
     212                 :            :  *
     213                 :            :  * @param[in] name
     214                 :            :  *   Network device name.
     215                 :            :  * @param[in] family
     216                 :            :  *   Address family: AF_INET for IPv4 or AF_INET6 for IPv6.
     217                 :            :  *
     218                 :            :  * @return
     219                 :            :  *   1 when interface has a route, negative errno value in case of error and
     220                 :            :  *   0 otherwise.
     221                 :            :  */
     222                 :            : static int
     223                 :          0 : vdev_netvsc_has_route(const struct if_nameindex *iface,
     224                 :            :                       const unsigned char family)
     225                 :            : {
     226                 :            :         /*
     227                 :            :          * The implementation can be simpler by getifaddrs() function usage but
     228                 :            :          * it works for IPv6 only starting from glibc 2.3.3.
     229                 :            :          */
     230                 :            :         char buf[4096];
     231                 :            :         int len;
     232                 :            :         int ret = 0;
     233                 :            :         int res;
     234                 :            :         int sock;
     235                 :            :         struct nlmsghdr *retmsg = (struct nlmsghdr *)buf;
     236                 :            :         struct sockaddr_nl sa;
     237                 :            :         struct {
     238                 :            :                 struct nlmsghdr nlhdr;
     239                 :            :                 struct ifaddrmsg addrmsg;
     240                 :            :         } msg;
     241                 :            : 
     242   [ #  #  #  # ]:          0 :         if (!iface || (family != AF_INET && family != AF_INET6)) {
     243                 :          0 :                 DRV_LOG(ERR, "%s", rte_strerror(EINVAL));
     244                 :          0 :                 return -EINVAL;
     245                 :            :         }
     246                 :          0 :         sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
     247         [ #  # ]:          0 :         if (sock == -1) {
     248                 :          0 :                 DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
     249                 :          0 :                 return -errno;
     250                 :            :         }
     251                 :            :         memset(&sa, 0, sizeof(sa));
     252                 :          0 :         sa.nl_family = AF_NETLINK;
     253                 :          0 :         sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
     254                 :          0 :         res = bind(sock, (struct sockaddr *)&sa, sizeof(sa));
     255         [ #  # ]:          0 :         if (res == -1) {
     256                 :          0 :                 ret = -errno;
     257                 :          0 :                 DRV_LOG(ERR, "cannot bind socket: %s", rte_strerror(errno));
     258                 :          0 :                 goto close;
     259                 :            :         }
     260                 :            :         memset(&msg, 0, sizeof(msg));
     261                 :          0 :         msg.nlhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
     262                 :          0 :         msg.nlhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
     263                 :          0 :         msg.nlhdr.nlmsg_type = RTM_GETADDR;
     264                 :          0 :         msg.nlhdr.nlmsg_pid = getpid();
     265                 :          0 :         msg.addrmsg.ifa_family = family;
     266                 :          0 :         msg.addrmsg.ifa_index = iface->if_index;
     267                 :          0 :         res = send(sock, &msg, msg.nlhdr.nlmsg_len, 0);
     268         [ #  # ]:          0 :         if (res == -1) {
     269                 :          0 :                 ret = -errno;
     270                 :          0 :                 DRV_LOG(ERR, "cannot send socket message: %s",
     271                 :            :                         rte_strerror(errno));
     272                 :          0 :                 goto close;
     273                 :            :         }
     274                 :            :         memset(buf, 0, sizeof(buf));
     275                 :          0 :         len = recv(sock, buf, sizeof(buf), 0);
     276         [ #  # ]:          0 :         if (len == -1) {
     277                 :          0 :                 ret = -errno;
     278                 :          0 :                 DRV_LOG(ERR, "cannot receive socket message: %s",
     279                 :            :                         rte_strerror(errno));
     280                 :          0 :                 goto close;
     281                 :            :         }
     282   [ #  #  #  #  :          0 :         while (NLMSG_OK(retmsg, (unsigned int)len)) {
                   #  # ]
     283                 :            :                 struct ifaddrmsg *retaddr =
     284                 :            :                                 (struct ifaddrmsg *)NLMSG_DATA(retmsg);
     285                 :            : 
     286         [ #  # ]:          0 :                 if (retaddr->ifa_family == family &&
     287         [ #  # ]:          0 :                     retaddr->ifa_index == iface->if_index) {
     288                 :          0 :                         struct rtattr *retrta = IFA_RTA(retaddr);
     289                 :          0 :                         int attlen = IFA_PAYLOAD(retmsg);
     290                 :            : 
     291   [ #  #  #  #  :          0 :                         while (RTA_OK(retrta, attlen)) {
                   #  # ]
     292         [ #  # ]:          0 :                                 if (retrta->rta_type == IFA_ADDRESS) {
     293                 :            :                                         ret = 1;
     294                 :          0 :                                         DRV_LOG(DEBUG, "interface %s has IP",
     295                 :            :                                                 iface->if_name);
     296                 :          0 :                                         goto close;
     297                 :            :                                 }
     298                 :          0 :                                 retrta = RTA_NEXT(retrta, attlen);
     299                 :            :                         }
     300                 :            :                 }
     301                 :          0 :                 retmsg = NLMSG_NEXT(retmsg, len);
     302                 :            :         }
     303                 :          0 : close:
     304                 :          0 :         close(sock);
     305                 :          0 :         return ret;
     306                 :            : }
     307                 :            : 
     308                 :            : /**
     309                 :            :  * Retrieve network interface data from sysfs symbolic link.
     310                 :            :  *
     311                 :            :  * @param[out] buf
     312                 :            :  *   Output data buffer.
     313                 :            :  * @param size
     314                 :            :  *   Output buffer size.
     315                 :            :  * @param[in] if_name
     316                 :            :  *   Netdevice name.
     317                 :            :  * @param[in] relpath
     318                 :            :  *   Symbolic link path relative to netdevice sysfs entry.
     319                 :            :  *
     320                 :            :  * @return
     321                 :            :  *   0 on success, a negative error code otherwise.
     322                 :            :  */
     323                 :            : static int
     324                 :          0 : vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
     325                 :            :                            const char *relpath)
     326                 :          0 : {
     327                 :            :         struct vdev_netvsc_ctx *ctx;
     328         [ #  # ]:          0 :         char in[RTE_MAX(sizeof(ctx->yield), 256u)];
     329                 :            :         int ret;
     330                 :            : 
     331                 :            :         ret = snprintf(in, sizeof(in), "/sys/class/net/%s/%s",
     332                 :            :                        if_name, relpath);
     333   [ #  #  #  # ]:          0 :         if (ret == -1 || (size_t)ret >= sizeof(in))
     334                 :            :                 return -ENOBUFS;
     335                 :          0 :         ret = readlink(in, buf, size);
     336         [ #  # ]:          0 :         if (ret == -1)
     337                 :          0 :                 return -errno;
     338         [ #  # ]:          0 :         if ((size_t)ret >= size - 1)
     339                 :            :                 return -ENOBUFS;
     340                 :          0 :         buf[ret] = '\0';
     341                 :          0 :         return 0;
     342                 :            : }
     343                 :            : 
     344                 :            : /**
     345                 :            :  * Probe a network interface to associate with vdev_netvsc context.
     346                 :            :  *
     347                 :            :  * This function determines if the network device matches the properties of
     348                 :            :  * the NetVSC interface associated with the vdev_netvsc context and
     349                 :            :  * communicates its bus address to the fail-safe PMD instance if so.
     350                 :            :  *
     351                 :            :  * It is normally used with vdev_netvsc_foreach_iface().
     352                 :            :  *
     353                 :            :  * @param[in] iface
     354                 :            :  *   Pointer to netdevice description structure (name and index).
     355                 :            :  * @param[in] eth_addr
     356                 :            :  *   MAC address associated with @p iface.
     357                 :            :  * @param ap
     358                 :            :  *   Variable arguments list comprising:
     359                 :            :  *
     360                 :            :  *   - struct vdev_netvsc_ctx *ctx:
     361                 :            :  *     Context to associate network interface with.
     362                 :            :  *
     363                 :            :  * @return
     364                 :            :  *   A nonzero value when interface matches, 0 otherwise or in case of
     365                 :            :  *   error.
     366                 :            :  */
     367                 :            : static int
     368                 :          0 : vdev_netvsc_device_probe(const struct if_nameindex *iface,
     369                 :            :                     const struct rte_ether_addr *eth_addr,
     370                 :            :                     va_list ap)
     371                 :          0 : {
     372                 :          0 :         struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
     373                 :          0 :         char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
     374                 :            :         const char *addr;
     375                 :            :         size_t len;
     376                 :            :         int ret;
     377                 :            : 
     378                 :            :         /* Skip non-matching or unwanted NetVSC interfaces. */
     379         [ #  # ]:          0 :         if (ctx->if_index == iface->if_index) {
     380         [ #  # ]:          0 :                 if (!strcmp(ctx->if_name, iface->if_name))
     381                 :            :                         return 0;
     382                 :          0 :                 DRV_LOG(DEBUG,
     383                 :            :                         "NetVSC interface \"%s\" (index %u) renamed \"%s\"",
     384                 :            :                         ctx->if_name, ctx->if_index, iface->if_name);
     385                 :          0 :                 strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
     386                 :          0 :                 return 0;
     387                 :            :         }
     388         [ #  # ]:          0 :         if (!rte_is_same_ether_addr(eth_addr, &ctx->if_addr))
     389                 :            :                 return 0;
     390                 :            :         /* Look for associated PCI device. */
     391                 :          0 :         ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
     392                 :            :                                          "device/subsystem");
     393         [ #  # ]:          0 :         if (ret)
     394                 :            :                 return 0;
     395                 :          0 :         addr = strrchr(buf, '/');
     396         [ #  # ]:          0 :         addr = addr ? addr + 1 : buf;
     397         [ #  # ]:          0 :         if (strcmp(addr, "pci"))
     398                 :            :                 return 0;
     399                 :          0 :         ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
     400                 :            :                                          "device");
     401         [ #  # ]:          0 :         if (ret)
     402                 :            :                 return 0;
     403                 :          0 :         addr = strrchr(buf, '/');
     404         [ #  # ]:          0 :         addr = addr ? addr + 1 : buf;
     405                 :          0 :         len = strlen(addr);
     406         [ #  # ]:          0 :         if (!len)
     407                 :            :                 return 0;
     408                 :            :         /* Send PCI device argument to fail-safe PMD instance. */
     409         [ #  # ]:          0 :         if (strcmp(addr, ctx->yield))
     410                 :          0 :                 DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
     411                 :            :                         " interface \"%s\" (index %u)", addr, ctx->if_name,
     412                 :            :                         ctx->if_index);
     413                 :          0 :         memmove(buf, addr, len + 1);
     414                 :            :         addr = buf;
     415                 :          0 :         buf[len] = '\n';
     416                 :          0 :         ret = write(ctx->pipe[1], addr, len + 1);
     417                 :          0 :         buf[len] = '\0';
     418         [ #  # ]:          0 :         if (ret == -1) {
     419         [ #  # ]:          0 :                 if (errno == EINTR || errno == EAGAIN)
     420                 :            :                         return 1;
     421                 :          0 :                 DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
     422                 :            :                         " interface \"%s\": %s", addr, ctx->if_name,
     423                 :            :                         rte_strerror(errno));
     424                 :          0 :                 return 1;
     425                 :            :         }
     426         [ #  # ]:          0 :         if ((size_t)ret != len + 1) {
     427                 :            :                 /*
     428                 :            :                  * Attempt to override previous partial write, no need to
     429                 :            :                  * recover if that fails.
     430                 :            :                  */
     431                 :          0 :                 ret = write(ctx->pipe[1], "\n", 1);
     432                 :            :                 (void)ret;
     433                 :          0 :                 return 1;
     434                 :            :         }
     435                 :          0 :         fsync(ctx->pipe[1]);
     436                 :            :         memcpy(ctx->yield, addr, len + 1);
     437                 :          0 :         return 1;
     438                 :            : }
     439                 :            : 
     440                 :            : /**
     441                 :            :  * Alarm callback that regularly probes system network interfaces.
     442                 :            :  *
     443                 :            :  * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
     444                 :            :  * long as an vdev_netvsc context instance exists.
     445                 :            :  *
     446                 :            :  * @param arg
     447                 :            :  *   Ignored.
     448                 :            :  */
     449                 :            : static void
     450                 :          0 : vdev_netvsc_alarm(__rte_unused void *arg)
     451                 :            : {
     452                 :            :         struct vdev_netvsc_ctx *ctx;
     453                 :            :         int ret;
     454                 :            : 
     455         [ #  # ]:          0 :         LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
     456                 :          0 :                 ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0,
     457                 :            :                       ctx);
     458         [ #  # ]:          0 :                 if (ret < 0)
     459                 :            :                         break;
     460                 :            :         }
     461         [ #  # ]:          0 :         if (!vdev_netvsc_ctx_count)
     462                 :            :                 return;
     463                 :          0 :         ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
     464                 :            :                                 vdev_netvsc_alarm, NULL);
     465         [ #  # ]:          0 :         if (ret < 0) {
     466                 :          0 :                 DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
     467                 :            :                         rte_strerror(-ret));
     468                 :            :         }
     469                 :            : }
     470                 :            : 
     471                 :            : /**
     472                 :            :  * Probe a NetVSC interface to generate a vdev_netvsc context from.
     473                 :            :  *
     474                 :            :  * This function instantiates vdev_netvsc contexts either for all NetVSC
     475                 :            :  * devices found on the system or only a subset provided as device
     476                 :            :  * arguments.
     477                 :            :  *
     478                 :            :  * It is normally used with vdev_netvsc_foreach_iface().
     479                 :            :  *
     480                 :            :  * @param[in] iface
     481                 :            :  *   Pointer to netdevice description structure (name and index).
     482                 :            :  * @param[in] eth_addr
     483                 :            :  *   MAC address associated with @p iface.
     484                 :            :  * @param ap
     485                 :            :  *   Variable arguments list comprising:
     486                 :            :  *
     487                 :            :  *   - const char *name:
     488                 :            :  *     Name associated with current driver instance.
     489                 :            :  *
     490                 :            :  *   - struct rte_kvargs *kvargs:
     491                 :            :  *     Device arguments provided to current driver instance.
     492                 :            :  *
     493                 :            :  *   - int force:
     494                 :            :  *     Accept specified interface even if not detected as NetVSC.
     495                 :            :  *
     496                 :            :  *   - unsigned int specified:
     497                 :            :  *     Number of specific netdevices provided as device arguments.
     498                 :            :  *
     499                 :            :  *   - unsigned int *matched:
     500                 :            :  *     The number of specified netdevices matched by this function.
     501                 :            :  *
     502                 :            :  * @return
     503                 :            :  *   A nonzero value when interface matches, 0 otherwise or in case of
     504                 :            :  *   error.
     505                 :            :  */
     506                 :            : static int
     507                 :          0 : vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
     508                 :            :                          const struct rte_ether_addr *eth_addr,
     509                 :            :                          va_list ap)
     510                 :            : {
     511                 :          0 :         const char *name = va_arg(ap, const char *);
     512                 :          0 :         struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
     513                 :          0 :         unsigned int specified = va_arg(ap, unsigned int);
     514                 :          0 :         unsigned int *matched = va_arg(ap, unsigned int *);
     515                 :            :         unsigned int i;
     516                 :            :         struct vdev_netvsc_ctx *ctx;
     517                 :            :         int ret;
     518                 :            : 
     519                 :            :         /* Probe all interfaces when none are specified. */
     520         [ #  # ]:          0 :         if (specified) {
     521         [ #  # ]:          0 :                 for (i = 0; i != kvargs->count; ++i) {
     522                 :            :                         const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
     523                 :            : 
     524         [ #  # ]:          0 :                         if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
     525         [ #  # ]:          0 :                                 if (!strcmp(pair->value, iface->if_name))
     526                 :            :                                         break;
     527         [ #  # ]:          0 :                         } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
     528                 :            :                                 struct rte_ether_addr tmp;
     529                 :            : 
     530         [ #  # ]:          0 :                                 if (rte_ether_unformat_addr(pair->value, &tmp) != 0) {
     531                 :          0 :                                         DRV_LOG(ERR,
     532                 :            :                                                 "invalid MAC address format"
     533                 :            :                                                 " \"%s\"",
     534                 :            :                                                 pair->value);
     535                 :          0 :                                         return -EINVAL;
     536                 :            :                                 }
     537         [ #  # ]:          0 :                                 if (rte_is_same_ether_addr(eth_addr, &tmp))
     538                 :            :                                         break;
     539                 :            :                         }
     540                 :            :                 }
     541         [ #  # ]:          0 :                 if (i == kvargs->count)
     542                 :            :                         return 0;
     543                 :          0 :                 ++(*matched);
     544                 :            :         }
     545                 :            :         /* Weed out interfaces already handled. */
     546         [ #  # ]:          0 :         LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
     547         [ #  # ]:          0 :                 if (ctx->if_index == iface->if_index)
     548                 :            :                         break;
     549         [ #  # ]:          0 :         if (ctx) {
     550         [ #  # ]:          0 :                 if (!specified)
     551                 :            :                         return 0;
     552                 :          0 :                 DRV_LOG(WARNING,
     553                 :            :                         "interface \"%s\" (index %u) is already handled,"
     554                 :            :                         " skipping",
     555                 :            :                         iface->if_name, iface->if_index);
     556                 :          0 :                 return 0;
     557                 :            :         }
     558                 :            :         /* Routed NetVSC should not be probed. */
     559   [ #  #  #  # ]:          0 :         if (vdev_netvsc_has_route(iface, AF_INET) ||
     560                 :          0 :             vdev_netvsc_has_route(iface, AF_INET6)) {
     561         [ #  # ]:          0 :                 if (!specified)
     562                 :            :                         return 0;
     563                 :          0 :                 DRV_LOG(WARNING, "probably using routed NetVSC interface \"%s\""
     564                 :            :                         " (index %u)", iface->if_name, iface->if_index);
     565                 :            :         }
     566                 :            :         /* Create interface context. */
     567                 :          0 :         ctx = calloc(1, sizeof(*ctx));
     568         [ #  # ]:          0 :         if (!ctx) {
     569                 :          0 :                 ret = -errno;
     570                 :          0 :                 DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
     571                 :            :                         iface->if_name, rte_strerror(errno));
     572                 :          0 :                 goto error;
     573                 :            :         }
     574                 :          0 :         ctx->id = vdev_netvsc_ctx_count;
     575                 :          0 :         strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
     576                 :          0 :         ctx->if_index = iface->if_index;
     577                 :          0 :         ctx->if_addr = *eth_addr;
     578                 :          0 :         ctx->pipe[0] = -1;
     579                 :          0 :         ctx->pipe[1] = -1;
     580                 :          0 :         ctx->yield[0] = '\0';
     581         [ #  # ]:          0 :         if (pipe(ctx->pipe) == -1) {
     582                 :          0 :                 ret = -errno;
     583                 :          0 :                 DRV_LOG(ERR,
     584                 :            :                         "cannot allocate control pipe for interface \"%s\": %s",
     585                 :            :                         ctx->if_name, rte_strerror(errno));
     586                 :          0 :                 goto error;
     587                 :            :         }
     588         [ #  # ]:          0 :         for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
     589                 :          0 :                 int flf = fcntl(ctx->pipe[i], F_GETFL);
     590                 :            : 
     591   [ #  #  #  # ]:          0 :                 if (flf != -1 &&
     592                 :          0 :                     fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
     593                 :            :                         continue;
     594                 :          0 :                 ret = -errno;
     595                 :          0 :                 DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
     596                 :            :                         " descriptor #%u (%d): %s", i, ctx->pipe[i],
     597                 :            :                         rte_strerror(errno));
     598                 :          0 :                 goto error;
     599                 :            :         }
     600                 :            :         /* Generate virtual device name and arguments. */
     601                 :            :         i = 0;
     602         [ #  # ]:          0 :         ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
     603                 :            :                        name, ctx->id);
     604         [ #  # ]:          0 :         if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
     605                 :            :                 ++i;
     606         [ #  # ]:          0 :         ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_vsc%u",
     607                 :            :                        ctx->id);
     608         [ #  # ]:          0 :         if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
     609                 :          0 :                 ++i;
     610         [ #  # ]:          0 :         ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
     611                 :            :                        "fd(%d),dev(net_tap_vsc%u,remote=%s)",
     612                 :            :                        ctx->pipe[0], ctx->id, ctx->if_name);
     613         [ #  # ]:          0 :         if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
     614                 :          0 :                 ++i;
     615         [ #  # ]:          0 :         if (i) {
     616                 :            :                 ret = -ENOBUFS;
     617                 :          0 :                 DRV_LOG(ERR, "generated virtual device name or argument list"
     618                 :            :                         " too long for interface \"%s\"", ctx->if_name);
     619                 :          0 :                 goto error;
     620                 :            :         }
     621                 :            :         /* Request virtual device generation. */
     622                 :          0 :         DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
     623                 :            :                 ctx->devname, ctx->devargs);
     624                 :          0 :         vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0, ctx);
     625                 :          0 :         ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
     626         [ #  # ]:          0 :         if (ret < 0)
     627                 :          0 :                 goto error;
     628         [ #  # ]:          0 :         LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
     629                 :          0 :         ++vdev_netvsc_ctx_count;
     630                 :          0 :         DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
     631                 :            :                 ctx->if_name);
     632                 :          0 :         return 0;
     633                 :          0 : error:
     634         [ #  # ]:          0 :         if (ctx)
     635                 :          0 :                 vdev_netvsc_ctx_destroy(ctx);
     636                 :            :         return ret;
     637                 :            : }
     638                 :            : 
     639                 :            : /**
     640                 :            :  * Probe NetVSC interfaces.
     641                 :            :  *
     642                 :            :  * This function probes system netdevices according to the specified device
     643                 :            :  * arguments and starts a periodic alarm callback to notify the resulting
     644                 :            :  * fail-safe PMD instances of their sub-devices whereabouts.
     645                 :            :  *
     646                 :            :  * @param dev
     647                 :            :  *   Virtual device context for driver instance.
     648                 :            :  *
     649                 :            :  * @return
     650                 :            :  *    Always 0, even in case of errors.
     651                 :            :  */
     652                 :            : static int
     653         [ #  # ]:          0 : vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
     654                 :            : {
     655                 :            :         static const char *const vdev_netvsc_arg[] = {
     656                 :            :                 VDEV_NETVSC_ARG_IFACE,
     657                 :            :                 VDEV_NETVSC_ARG_MAC,
     658                 :            :                 VDEV_NETVSC_ARG_FORCE,
     659                 :            :                 VDEV_NETVSC_ARG_IGNORE,
     660                 :            :                 NULL,
     661                 :            :         };
     662                 :            :         const char *name = rte_vdev_device_name(dev);
     663                 :            :         const char *args = rte_vdev_device_args(dev);
     664         [ #  # ]:          0 :         struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
     665                 :            :                                                      vdev_netvsc_arg);
     666                 :            :         unsigned int specified = 0;
     667                 :          0 :         unsigned int matched = 0;
     668                 :            :         int force = 0;
     669                 :            :         int ignore = 0;
     670                 :            :         unsigned int i;
     671                 :            :         int ret;
     672                 :            : 
     673                 :          0 :         DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
     674                 :          0 :         rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
     675         [ #  # ]:          0 :         if (!kvargs) {
     676                 :          0 :                 DRV_LOG(ERR, "cannot parse arguments list");
     677                 :          0 :                 goto error;
     678                 :            :         }
     679         [ #  # ]:          0 :         for (i = 0; i != kvargs->count; ++i) {
     680                 :            :                 const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
     681                 :            : 
     682         [ #  # ]:          0 :                 if (!strcmp(pair->key, VDEV_NETVSC_ARG_FORCE))
     683                 :          0 :                         force = !!atoi(pair->value);
     684         [ #  # ]:          0 :                 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IGNORE))
     685                 :          0 :                         ignore = !!atoi(pair->value);
     686         [ #  # ]:          0 :                 else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
     687         [ #  # ]:          0 :                          !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
     688                 :          0 :                         ++specified;
     689                 :            :         }
     690         [ #  # ]:          0 :         if (ignore)
     691                 :          0 :                 goto ignore;
     692         [ #  # ]:          0 :         if (specified > 1) {
     693                 :          0 :                 DRV_LOG(ERR, "More than one way used to specify the netvsc"
     694                 :            :                         " device.");
     695                 :          0 :                 goto error;
     696                 :            :         }
     697                 :            :         /* Gather interfaces. */
     698                 :          0 :         ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 1, name,
     699                 :            :                                         kvargs, specified, &matched);
     700         [ #  # ]:          0 :         if (ret < 0)
     701                 :          0 :                 goto error;
     702   [ #  #  #  # ]:          0 :         if (specified && matched < specified) {
     703         [ #  # ]:          0 :                 if (!force) {
     704                 :          0 :                         DRV_LOG(ERR, "Cannot find the specified netvsc device");
     705                 :          0 :                         goto error;
     706                 :            :                 }
     707                 :            :                 /* Try to force probing on non-netvsc specified device. */
     708         [ #  # ]:          0 :                 if (vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 0, name,
     709                 :            :                                               kvargs, specified, &matched) < 0)
     710                 :          0 :                         goto error;
     711         [ #  # ]:          0 :                 if (matched < specified) {
     712                 :          0 :                         DRV_LOG(ERR, "Cannot find the specified device");
     713                 :          0 :                         goto error;
     714                 :            :                 }
     715                 :          0 :                 DRV_LOG(WARNING, "non-netvsc device was probed as netvsc");
     716                 :            :         }
     717                 :          0 : error:
     718                 :          0 :         ++vdev_netvsc_ctx_inst;
     719                 :          0 : ignore:
     720                 :          0 :         rte_kvargs_free(kvargs);
     721                 :            :         /* Reset alarm if there are device context created */
     722         [ #  # ]:          0 :         if (vdev_netvsc_ctx_count) {
     723                 :          0 :                 ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
     724                 :            :                                         vdev_netvsc_alarm, NULL);
     725         [ #  # ]:          0 :                 if (ret < 0)
     726                 :          0 :                         DRV_LOG(ERR, "unable to schedule alarm callback: %s",
     727                 :            :                                 rte_strerror(-ret));
     728                 :            :         }
     729                 :          0 :         return 0;
     730                 :            : }
     731                 :            : 
     732                 :            : /**
     733                 :            :  * Remove driver instance.
     734                 :            :  *
     735                 :            :  * The alarm callback and underlying vdev_netvsc context instances are only
     736                 :            :  * destroyed after the last PMD instance is removed.
     737                 :            :  *
     738                 :            :  * @param dev
     739                 :            :  *   Virtual device context for driver instance.
     740                 :            :  *
     741                 :            :  * @return
     742                 :            :  *   Always 0.
     743                 :            :  */
     744                 :            : static int
     745                 :          0 : vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
     746                 :            : {
     747         [ #  # ]:          0 :         if (--vdev_netvsc_ctx_inst)
     748                 :            :                 return 0;
     749                 :          0 :         rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
     750         [ #  # ]:          0 :         while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
     751                 :            :                 struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
     752                 :            : 
     753         [ #  # ]:          0 :                 LIST_REMOVE(ctx, entry);
     754                 :          0 :                 --vdev_netvsc_ctx_count;
     755                 :          0 :                 vdev_netvsc_ctx_destroy(ctx);
     756                 :            :         }
     757                 :            :         return 0;
     758                 :            : }
     759                 :            : 
     760                 :            : /** Virtual device descriptor. */
     761                 :            : static struct rte_vdev_driver vdev_netvsc_vdev = {
     762                 :            :         .probe = vdev_netvsc_vdev_probe,
     763                 :            :         .remove = vdev_netvsc_vdev_remove,
     764                 :            : };
     765                 :            : 
     766                 :        235 : RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
     767                 :            : RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
     768                 :            : RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
     769                 :            :                               VDEV_NETVSC_ARG_IFACE "=<string> "
     770                 :            :                               VDEV_NETVSC_ARG_MAC "=<string> "
     771                 :            :                               VDEV_NETVSC_ARG_FORCE "=<int> "
     772                 :            :                               VDEV_NETVSC_ARG_IGNORE "=<int>");
     773                 :            : 
     774                 :            : /** Compare function for vdev find device operation. */
     775                 :            : static int
     776                 :          0 : vdev_netvsc_cmp_rte_device(const struct rte_device *dev1,
     777                 :            :                            __rte_unused const void *_dev2)
     778                 :            : {
     779                 :          0 :         return strncmp(dev1->devargs->name, VDEV_NETVSC_DRIVER_NAME,
     780                 :            :                        VDEV_NETVSC_DRIVER_NAME_LEN);
     781                 :            : }
     782                 :            : 
     783                 :            : /**
     784                 :            :  * A callback called by vdev bus scan function to ensure this driver probing
     785                 :            :  * automatically in Hyper-V VM system unless it already exists in the
     786                 :            :  * devargs list.
     787                 :            :  */
     788                 :            : static void
     789                 :          0 : vdev_netvsc_scan_callback(__rte_unused void *arg)
     790                 :            : {
     791                 :            :         struct rte_device *dev;
     792                 :            :         struct rte_devargs *devargs;
     793                 :          0 :         struct rte_bus *vbus = rte_bus_find_by_name("vdev");
     794                 :            : 
     795         [ #  # ]:          0 :         RTE_EAL_DEVARGS_FOREACH("vdev", devargs)
     796         [ #  # ]:          0 :                 if (!strncmp(devargs->name, VDEV_NETVSC_DRIVER_NAME,
     797                 :            :                              VDEV_NETVSC_DRIVER_NAME_LEN))
     798                 :            :                         return;
     799                 :            : 
     800                 :          0 :         dev = vbus->find_device(NULL, vdev_netvsc_cmp_rte_device,
     801                 :            :                                 VDEV_NETVSC_DRIVER_NAME);
     802         [ #  # ]:          0 :         if (dev)
     803                 :            :                 return;
     804         [ #  # ]:          0 :         if (rte_devargs_add(RTE_DEVTYPE_VIRTUAL, VDEV_NETVSC_DRIVER_NAME))
     805                 :          0 :                 DRV_LOG(ERR, "unable to add netvsc devargs.");
     806                 :            : }
     807                 :            : 
     808                 :            : /** Initialize the custom scan. */
     809                 :        235 : RTE_INIT(vdev_netvsc_custom_scan_add)
     810                 :            : {
     811         [ -  + ]:        235 :         if (rte_hypervisor_get() == RTE_HYPERVISOR_HYPERV)
     812                 :          0 :                 rte_vdev_add_custom_scan(vdev_netvsc_scan_callback, NULL);
     813                 :        235 : }

Generated by: LCOV version 1.14