LCOV - code coverage report
Current view: top level - drivers/common/mlx5/linux - mlx5_nl.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 672 0.0 %
Date: 2025-05-01 17:49:45 Functions: 0 40 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 353 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright 2018 6WIND S.A.
       3                 :            :  * Copyright 2018 Mellanox Technologies, Ltd
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <errno.h>
       7                 :            : #include <linux/if_link.h>
       8                 :            : #include <linux/rtnetlink.h>
       9                 :            : #include <linux/genetlink.h>
      10                 :            : #include <net/if.h>
      11                 :            : #include <rdma/rdma_netlink.h>
      12                 :            : #include <stdbool.h>
      13                 :            : #include <stdint.h>
      14                 :            : #include <stdlib.h>
      15                 :            : #include <stdalign.h>
      16                 :            : #include <string.h>
      17                 :            : #include <sys/socket.h>
      18                 :            : #include <unistd.h>
      19                 :            : 
      20                 :            : #include <eal_export.h>
      21                 :            : #include <rte_errno.h>
      22                 :            : 
      23                 :            : #include "mlx5_nl.h"
      24                 :            : #include "../mlx5_common_log.h"
      25                 :            : #include "mlx5_malloc.h"
      26                 :            : #ifdef HAVE_DEVLINK
      27                 :            : #include <linux/devlink.h>
      28                 :            : #endif
      29                 :            : 
      30                 :            : 
      31                 :            : /* Size of the buffer to receive kernel messages */
      32                 :            : #define MLX5_NL_BUF_SIZE (32 * 1024)
      33                 :            : /* Send buffer size for the Netlink socket */
      34                 :            : #define MLX5_SEND_BUF_SIZE 32768
      35                 :            : /* Receive buffer size for the Netlink socket */
      36                 :            : #define MLX5_RECV_BUF_SIZE 32768
      37                 :            : /* Maximal physical port name length. */
      38                 :            : #define MLX5_PHYS_PORT_NAME_MAX 128
      39                 :            : 
      40                 :            : /** Parameters of VLAN devices created by driver. */
      41                 :            : #define MLX5_VMWA_VLAN_DEVICE_PFX "evmlx"
      42                 :            : /*
      43                 :            :  * Define NDA_RTA as defined in iproute2 sources.
      44                 :            :  *
      45                 :            :  * see in iproute2 sources file include/libnetlink.h
      46                 :            :  */
      47                 :            : #ifndef MLX5_NDA_RTA
      48                 :            : #define MLX5_NDA_RTA(r) \
      49                 :            :         ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
      50                 :            : #endif
      51                 :            : /*
      52                 :            :  * Define NLMSG_TAIL as defined in iproute2 sources.
      53                 :            :  *
      54                 :            :  * see in iproute2 sources file include/libnetlink.h
      55                 :            :  */
      56                 :            : #ifndef NLMSG_TAIL
      57                 :            : #define NLMSG_TAIL(nmsg) \
      58                 :            :         ((struct rtattr *)(((char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
      59                 :            : #endif
      60                 :            : /*
      61                 :            :  * The following definitions are normally found in rdma/rdma_netlink.h,
      62                 :            :  * however they are so recent that most systems do not expose them yet.
      63                 :            :  */
      64                 :            : #ifndef HAVE_RDMA_NL_NLDEV
      65                 :            : #define RDMA_NL_NLDEV 5
      66                 :            : #endif
      67                 :            : #ifndef HAVE_RDMA_NLDEV_CMD_GET
      68                 :            : #define RDMA_NLDEV_CMD_GET 1
      69                 :            : #endif
      70                 :            : #ifndef HAVE_RDMA_NLDEV_CMD_PORT_GET
      71                 :            : #define RDMA_NLDEV_CMD_PORT_GET 5
      72                 :            : #endif
      73                 :            : #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_INDEX
      74                 :            : #define RDMA_NLDEV_ATTR_DEV_INDEX 1
      75                 :            : #endif
      76                 :            : #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_NAME
      77                 :            : #define RDMA_NLDEV_ATTR_DEV_NAME 2
      78                 :            : #endif
      79                 :            : #ifndef HAVE_RDMA_NLDEV_ATTR_PORT_INDEX
      80                 :            : #define RDMA_NLDEV_ATTR_PORT_INDEX 3
      81                 :            : #endif
      82                 :            : #ifndef HAVE_RDMA_NLDEV_ATTR_PORT_STATE
      83                 :            : #define RDMA_NLDEV_ATTR_PORT_STATE 12
      84                 :            : #endif
      85                 :            : #ifndef HAVE_RDMA_NLDEV_ATTR_NDEV_INDEX
      86                 :            : #define RDMA_NLDEV_ATTR_NDEV_INDEX 50
      87                 :            : #endif
      88                 :            : #ifndef HAVE_RDMA_NLDEV_ATTR_EVENT_TYPE
      89                 :            : #define RDMA_NLDEV_ATTR_EVENT_TYPE 102
      90                 :            : #define RDMA_NETDEV_ATTACH_EVENT 2
      91                 :            : #define RDMA_NETDEV_DETACH_EVENT 3
      92                 :            : #endif
      93                 :            : #ifndef HAVE_RDMA_NLDEV_SYS_ATTR_MONITOR_MODE
      94                 :            : #define RDMA_NLDEV_SYS_ATTR_MONITOR_MODE 103
      95                 :            : #endif
      96                 :            : #ifndef HAVE_RDMA_NLDEV_CMD_MONITOR
      97                 :            : #define RDMA_NLDEV_CMD_MONITOR 28
      98                 :            : #endif
      99                 :            : #ifndef HAVE_RDMA_NLDEV_CMD_SYS_GET
     100                 :            : #define RDMA_NLDEV_CMD_SYS_GET 6
     101                 :            : #endif
     102                 :            : #ifndef HAVE_RDMA_NL_GROUP_NOTIFY
     103                 :            : #define RDMA_NL_GROUP_NOTIFY 4
     104                 :            : #endif
     105                 :            : #define RDMA_NL_GROUP_NOTIFICATION (1 << (RDMA_NL_GROUP_NOTIFY - 1))
     106                 :            : 
     107                 :            : /* These are normally found in linux/if_link.h. */
     108                 :            : #ifndef HAVE_IFLA_NUM_VF
     109                 :            : #define IFLA_NUM_VF 21
     110                 :            : #endif
     111                 :            : #ifndef HAVE_IFLA_EXT_MASK
     112                 :            : #define IFLA_EXT_MASK 29
     113                 :            : #endif
     114                 :            : #ifndef HAVE_IFLA_PHYS_SWITCH_ID
     115                 :            : #define IFLA_PHYS_SWITCH_ID 36
     116                 :            : #endif
     117                 :            : #ifndef HAVE_IFLA_PHYS_PORT_NAME
     118                 :            : #define IFLA_PHYS_PORT_NAME 38
     119                 :            : #endif
     120                 :            : 
     121                 :            : /*
     122                 :            :  * Some Devlink defines may be missed in old kernel versions,
     123                 :            :  * adjust used defines.
     124                 :            :  */
     125                 :            : #ifndef DEVLINK_GENL_NAME
     126                 :            : #define DEVLINK_GENL_NAME "devlink"
     127                 :            : #endif
     128                 :            : #ifndef DEVLINK_GENL_VERSION
     129                 :            : #define DEVLINK_GENL_VERSION 1
     130                 :            : #endif
     131                 :            : #ifndef DEVLINK_ATTR_BUS_NAME
     132                 :            : #define DEVLINK_ATTR_BUS_NAME 1
     133                 :            : #endif
     134                 :            : #ifndef DEVLINK_ATTR_DEV_NAME
     135                 :            : #define DEVLINK_ATTR_DEV_NAME 2
     136                 :            : #endif
     137                 :            : #ifndef DEVLINK_ATTR_PARAM
     138                 :            : #define DEVLINK_ATTR_PARAM 80
     139                 :            : #endif
     140                 :            : #ifndef DEVLINK_ATTR_PARAM_NAME
     141                 :            : #define DEVLINK_ATTR_PARAM_NAME 81
     142                 :            : #endif
     143                 :            : #ifndef DEVLINK_ATTR_PARAM_TYPE
     144                 :            : #define DEVLINK_ATTR_PARAM_TYPE 83
     145                 :            : #endif
     146                 :            : #ifndef DEVLINK_ATTR_PARAM_VALUES_LIST
     147                 :            : #define DEVLINK_ATTR_PARAM_VALUES_LIST 84
     148                 :            : #endif
     149                 :            : #ifndef DEVLINK_ATTR_PARAM_VALUE
     150                 :            : #define DEVLINK_ATTR_PARAM_VALUE 85
     151                 :            : #endif
     152                 :            : #ifndef DEVLINK_ATTR_PARAM_VALUE_DATA
     153                 :            : #define DEVLINK_ATTR_PARAM_VALUE_DATA 86
     154                 :            : #endif
     155                 :            : #ifndef DEVLINK_ATTR_PARAM_VALUE_CMODE
     156                 :            : #define DEVLINK_ATTR_PARAM_VALUE_CMODE 87
     157                 :            : #endif
     158                 :            : #ifndef DEVLINK_PARAM_CMODE_DRIVERINIT
     159                 :            : #define DEVLINK_PARAM_CMODE_DRIVERINIT 1
     160                 :            : #endif
     161                 :            : #ifndef DEVLINK_CMD_RELOAD
     162                 :            : #define DEVLINK_CMD_RELOAD 37
     163                 :            : #endif
     164                 :            : #ifndef DEVLINK_CMD_PARAM_GET
     165                 :            : #define DEVLINK_CMD_PARAM_GET 38
     166                 :            : #endif
     167                 :            : #ifndef DEVLINK_CMD_PARAM_SET
     168                 :            : #define DEVLINK_CMD_PARAM_SET 39
     169                 :            : #endif
     170                 :            : #ifndef NLA_FLAG
     171                 :            : #define NLA_FLAG 6
     172                 :            : #endif
     173                 :            : 
     174                 :            : /* Add/remove MAC address through Netlink */
     175                 :            : struct mlx5_nl_mac_addr {
     176                 :            :         struct rte_ether_addr (*mac)[];
     177                 :            :         /**< MAC address handled by the device. */
     178                 :            :         int mac_n; /**< Number of addresses in the array. */
     179                 :            : };
     180                 :            : 
     181                 :            : RTE_ATOMIC(uint32_t) atomic_sn;
     182                 :            : 
     183                 :            : /* Generate Netlink sequence number. */
     184                 :            : #define MLX5_NL_SN_GENERATE (rte_atomic_fetch_add_explicit(&atomic_sn, 1, \
     185                 :            :         rte_memory_order_relaxed) + 1)
     186                 :            : 
     187                 :            : /**
     188                 :            :  * Opens a Netlink socket.
     189                 :            :  *
     190                 :            :  * @param protocol
     191                 :            :  *   Netlink protocol (e.g. NETLINK_ROUTE, NETLINK_RDMA).
     192                 :            :  * @param groups
     193                 :            :  *   Groups to listen (e.g. RTMGRP_LINK), can be 0.
     194                 :            :  *
     195                 :            :  * @return
     196                 :            :  *   A file descriptor on success, a negative errno value otherwise and
     197                 :            :  *   rte_errno is set.
     198                 :            :  */
     199                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_init)
     200                 :            : int
     201                 :          0 : mlx5_nl_init(int protocol, int groups)
     202                 :            : {
     203                 :            :         int fd;
     204                 :            :         int buf_size;
     205                 :            :         socklen_t opt_size;
     206                 :          0 :         struct sockaddr_nl local = {
     207                 :            :                 .nl_family = AF_NETLINK,
     208                 :            :                 .nl_groups = groups,
     209                 :            :         };
     210                 :            :         int ret;
     211                 :            : 
     212                 :          0 :         fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
     213         [ #  # ]:          0 :         if (fd == -1) {
     214                 :          0 :                 rte_errno = errno;
     215                 :          0 :                 return -rte_errno;
     216                 :            :         }
     217                 :          0 :         opt_size = sizeof(buf_size);
     218                 :          0 :         ret = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf_size, &opt_size);
     219         [ #  # ]:          0 :         if (ret == -1) {
     220                 :          0 :                 rte_errno = errno;
     221                 :          0 :                 goto error;
     222                 :            :         }
     223                 :          0 :         DRV_LOG(DEBUG, "Netlink socket send buffer: %d", buf_size);
     224         [ #  # ]:          0 :         if (buf_size < MLX5_SEND_BUF_SIZE) {
     225                 :          0 :                 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
     226                 :            :                                  &buf_size, sizeof(buf_size));
     227         [ #  # ]:          0 :                 if (ret == -1) {
     228                 :          0 :                         rte_errno = errno;
     229                 :          0 :                         goto error;
     230                 :            :                 }
     231                 :            :         }
     232                 :          0 :         opt_size = sizeof(buf_size);
     233                 :          0 :         ret = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buf_size, &opt_size);
     234         [ #  # ]:          0 :         if (ret == -1) {
     235                 :          0 :                 rte_errno = errno;
     236                 :          0 :                 goto error;
     237                 :            :         }
     238                 :          0 :         DRV_LOG(DEBUG, "Netlink socket recv buffer: %d", buf_size);
     239         [ #  # ]:          0 :         if (buf_size < MLX5_RECV_BUF_SIZE) {
     240                 :          0 :                 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
     241                 :            :                                  &buf_size, sizeof(buf_size));
     242         [ #  # ]:          0 :                 if (ret == -1) {
     243                 :          0 :                         rte_errno = errno;
     244                 :          0 :                         goto error;
     245                 :            :                 }
     246                 :            :         }
     247                 :          0 :         ret = bind(fd, (struct sockaddr *)&local, sizeof(local));
     248         [ #  # ]:          0 :         if (ret == -1) {
     249                 :          0 :                 rte_errno = errno;
     250                 :          0 :                 goto error;
     251                 :            :         }
     252                 :            :         return fd;
     253                 :          0 : error:
     254                 :          0 :         close(fd);
     255                 :          0 :         return -rte_errno;
     256                 :            : }
     257                 :            : 
     258                 :            : /**
     259                 :            :  * Send a request message to the kernel on the Netlink socket.
     260                 :            :  *
     261                 :            :  * @param[in] nlsk_fd
     262                 :            :  *   Netlink socket file descriptor.
     263                 :            :  * @param[in] nh
     264                 :            :  *   The Netlink message send to the kernel.
     265                 :            :  * @param[in] ssn
     266                 :            :  *   Sequence number.
     267                 :            :  * @param[in] req
     268                 :            :  *   Pointer to the request structure.
     269                 :            :  * @param[in] len
     270                 :            :  *   Length of the request in bytes.
     271                 :            :  *
     272                 :            :  * @return
     273                 :            :  *   The number of sent bytes on success, a negative errno value otherwise and
     274                 :            :  *   rte_errno is set.
     275                 :            :  */
     276                 :            : static int
     277                 :          0 : mlx5_nl_request(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn, void *req,
     278                 :            :                 int len)
     279                 :            : {
     280                 :          0 :         struct sockaddr_nl sa = {
     281                 :            :                 .nl_family = AF_NETLINK,
     282                 :            :         };
     283                 :          0 :         struct iovec iov[2] = {
     284                 :            :                 { .iov_base = nh, .iov_len = sizeof(*nh), },
     285                 :            :                 { .iov_base = req, .iov_len = len, },
     286                 :            :         };
     287                 :          0 :         struct msghdr msg = {
     288                 :            :                 .msg_name = &sa,
     289                 :            :                 .msg_namelen = sizeof(sa),
     290                 :            :                 .msg_iov = iov,
     291                 :            :                 .msg_iovlen = 2,
     292                 :            :         };
     293                 :            :         int send_bytes;
     294                 :            : 
     295                 :          0 :         nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
     296                 :          0 :         nh->nlmsg_seq = sn;
     297                 :          0 :         send_bytes = sendmsg(nlsk_fd, &msg, 0);
     298         [ #  # ]:          0 :         if (send_bytes < 0) {
     299                 :          0 :                 rte_errno = errno;
     300                 :          0 :                 return -rte_errno;
     301                 :            :         }
     302                 :            :         return send_bytes;
     303                 :            : }
     304                 :            : 
     305                 :            : /**
     306                 :            :  * Send a message to the kernel on the Netlink socket.
     307                 :            :  *
     308                 :            :  * @param[in] nlsk_fd
     309                 :            :  *   The Netlink socket file descriptor used for communication.
     310                 :            :  * @param[in] nh
     311                 :            :  *   The Netlink message send to the kernel.
     312                 :            :  * @param[in] sn
     313                 :            :  *   Sequence number.
     314                 :            :  *
     315                 :            :  * @return
     316                 :            :  *   The number of sent bytes on success, a negative errno value otherwise and
     317                 :            :  *   rte_errno is set.
     318                 :            :  */
     319                 :            : static int
     320                 :          0 : mlx5_nl_send(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn)
     321                 :            : {
     322                 :          0 :         struct sockaddr_nl sa = {
     323                 :            :                 .nl_family = AF_NETLINK,
     324                 :            :         };
     325                 :          0 :         struct iovec iov = {
     326                 :            :                 .iov_base = nh,
     327                 :          0 :                 .iov_len = nh->nlmsg_len,
     328                 :            :         };
     329                 :          0 :         struct msghdr msg = {
     330                 :            :                 .msg_name = &sa,
     331                 :            :                 .msg_namelen = sizeof(sa),
     332                 :            :                 .msg_iov = &iov,
     333                 :            :                 .msg_iovlen = 1,
     334                 :            :         };
     335                 :            :         int send_bytes;
     336                 :            : 
     337                 :          0 :         nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
     338                 :          0 :         nh->nlmsg_seq = sn;
     339                 :          0 :         send_bytes = sendmsg(nlsk_fd, &msg, 0);
     340         [ #  # ]:          0 :         if (send_bytes < 0) {
     341                 :          0 :                 rte_errno = errno;
     342                 :          0 :                 return -rte_errno;
     343                 :            :         }
     344                 :            :         return send_bytes;
     345                 :            : }
     346                 :            : 
     347                 :            : /**
     348                 :            :  * Receive a message from the kernel on the Netlink socket, following
     349                 :            :  * mlx5_nl_send().
     350                 :            :  *
     351                 :            :  * @param[in] nlsk_fd
     352                 :            :  *   The Netlink socket file descriptor used for communication.
     353                 :            :  * @param[in] sn
     354                 :            :  *   Sequence number.
     355                 :            :  * @param[in] cb
     356                 :            :  *   The callback function to call for each Netlink message received.
     357                 :            :  * @param[in, out] arg
     358                 :            :  *   Custom arguments for the callback.
     359                 :            :  *
     360                 :            :  * @return
     361                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     362                 :            :  */
     363                 :            : static int
     364                 :          0 : mlx5_nl_recv(int nlsk_fd, uint32_t sn, int (*cb)(struct nlmsghdr *, void *arg),
     365                 :            :              void *arg)
     366                 :            : {
     367                 :            :         struct sockaddr_nl sa;
     368                 :            :         struct iovec iov;
     369                 :          0 :         struct msghdr msg = {
     370                 :            :                 .msg_name = &sa,
     371                 :            :                 .msg_namelen = sizeof(sa),
     372                 :            :                 .msg_iov = &iov,
     373                 :            :                 /* One message at a time */
     374                 :            :                 .msg_iovlen = 1,
     375                 :            :         };
     376                 :            :         void *buf = NULL;
     377                 :            :         int multipart = 0;
     378                 :            :         int ret = 0;
     379                 :            : 
     380                 :            :         do {
     381                 :            :                 struct nlmsghdr *nh;
     382                 :            :                 int recv_bytes;
     383                 :            : 
     384                 :            :                 do {
     385                 :            :                         /* Query length of incoming message. */
     386                 :          0 :                         iov.iov_base = NULL;
     387                 :          0 :                         iov.iov_len = 0;
     388                 :          0 :                         recv_bytes = recvmsg(nlsk_fd, &msg,
     389                 :            :                                              MSG_PEEK | MSG_TRUNC);
     390         [ #  # ]:          0 :                         if (recv_bytes < 0) {
     391                 :          0 :                                 rte_errno = errno;
     392                 :          0 :                                 ret = -rte_errno;
     393                 :          0 :                                 goto exit;
     394                 :            :                         }
     395         [ #  # ]:          0 :                         if (recv_bytes == 0) {
     396                 :          0 :                                 rte_errno = ENODATA;
     397                 :            :                                 ret = -rte_errno;
     398                 :          0 :                                 goto exit;
     399                 :            :                         }
     400                 :            :                         /* Allocate buffer to fetch the message. */
     401                 :            :                         if (recv_bytes < MLX5_RECV_BUF_SIZE)
     402                 :            :                                 recv_bytes = MLX5_RECV_BUF_SIZE;
     403                 :          0 :                         mlx5_free(buf);
     404                 :          0 :                         buf = mlx5_malloc(0, recv_bytes, 0, SOCKET_ID_ANY);
     405         [ #  # ]:          0 :                         if (!buf) {
     406                 :          0 :                                 rte_errno = ENOMEM;
     407                 :            :                                 ret = -rte_errno;
     408                 :          0 :                                 goto exit;
     409                 :            :                         }
     410                 :            :                         /* Fetch the message. */
     411                 :          0 :                         iov.iov_base = buf;
     412                 :          0 :                         iov.iov_len = recv_bytes;
     413                 :          0 :                         recv_bytes = recvmsg(nlsk_fd, &msg, 0);
     414         [ #  # ]:          0 :                         if (recv_bytes == -1) {
     415                 :          0 :                                 rte_errno = errno;
     416                 :          0 :                                 ret = -rte_errno;
     417                 :          0 :                                 goto exit;
     418                 :            :                         }
     419                 :            :                         nh = (struct nlmsghdr *)buf;
     420         [ #  # ]:          0 :                 } while (nh->nlmsg_seq != sn);
     421                 :            :                 for (;
     422   [ #  #  #  #  :          0 :                      NLMSG_OK(nh, (unsigned int)recv_bytes);
                   #  # ]
     423                 :          0 :                      nh = NLMSG_NEXT(nh, recv_bytes)) {
     424         [ #  # ]:          0 :                         if (nh->nlmsg_type == NLMSG_ERROR) {
     425                 :            :                                 struct nlmsgerr *err_data = NLMSG_DATA(nh);
     426                 :            : 
     427         [ #  # ]:          0 :                                 if (err_data->error < 0) {
     428                 :          0 :                                         rte_errno = -err_data->error;
     429                 :            :                                         ret = -rte_errno;
     430                 :          0 :                                         goto exit;
     431                 :            :                                 }
     432                 :            :                                 /* Ack message. */
     433                 :            :                                 ret = 0;
     434                 :          0 :                                 goto exit;
     435                 :            :                         }
     436                 :            :                         /* Multi-part msgs and their trailing DONE message. */
     437         [ #  # ]:          0 :                         if (nh->nlmsg_flags & NLM_F_MULTI) {
     438         [ #  # ]:          0 :                                 if (nh->nlmsg_type == NLMSG_DONE) {
     439                 :            :                                         ret =  0;
     440                 :          0 :                                         goto exit;
     441                 :            :                                 }
     442                 :            :                                 multipart = 1;
     443                 :            :                         }
     444         [ #  # ]:          0 :                         if (cb) {
     445                 :          0 :                                 ret = cb(nh, arg);
     446         [ #  # ]:          0 :                                 if (ret < 0)
     447                 :          0 :                                         goto exit;
     448                 :            :                         }
     449                 :            :                 }
     450         [ #  # ]:          0 :         } while (multipart);
     451                 :          0 : exit:
     452                 :          0 :         mlx5_free(buf);
     453                 :          0 :         return ret;
     454                 :            : }
     455                 :            : 
     456                 :            : /**
     457                 :            :  * Parse Netlink message to retrieve the bridge MAC address.
     458                 :            :  *
     459                 :            :  * @param nh
     460                 :            :  *   Pointer to Netlink Message Header.
     461                 :            :  * @param arg
     462                 :            :  *   PMD data register with this callback.
     463                 :            :  *
     464                 :            :  * @return
     465                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     466                 :            :  */
     467                 :            : static int
     468                 :          0 : mlx5_nl_mac_addr_cb(struct nlmsghdr *nh, void *arg)
     469                 :            : {
     470                 :            :         struct mlx5_nl_mac_addr *data = arg;
     471                 :            :         struct ndmsg *r = NLMSG_DATA(nh);
     472                 :            :         struct rtattr *attribute;
     473                 :            :         int len;
     474                 :            : 
     475                 :          0 :         len = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
     476                 :          0 :         for (attribute = MLX5_NDA_RTA(r);
     477   [ #  #  #  #  :          0 :              RTA_OK(attribute, len);
                   #  # ]
     478                 :          0 :              attribute = RTA_NEXT(attribute, len)) {
     479         [ #  # ]:          0 :                 if (attribute->rta_type == NDA_LLADDR) {
     480         [ #  # ]:          0 :                         if (data->mac_n == MLX5_MAX_MAC_ADDRESSES) {
     481                 :          0 :                                 DRV_LOG(WARNING,
     482                 :            :                                         "not enough room to finalize the"
     483                 :            :                                         " request");
     484                 :          0 :                                 rte_errno = ENOMEM;
     485                 :          0 :                                 return -rte_errno;
     486                 :            :                         }
     487                 :            : #ifdef RTE_LIBRTE_MLX5_DEBUG
     488                 :            :                         char m[RTE_ETHER_ADDR_FMT_SIZE];
     489                 :            : 
     490                 :            :                         rte_ether_format_addr(m, RTE_ETHER_ADDR_FMT_SIZE,
     491                 :            :                                               RTA_DATA(attribute));
     492                 :            :                         DRV_LOG(DEBUG, "bridge MAC address %s", m);
     493                 :            : #endif
     494                 :          0 :                         memcpy(&(*data->mac)[data->mac_n++],
     495                 :            :                                RTA_DATA(attribute), RTE_ETHER_ADDR_LEN);
     496                 :            :                 }
     497                 :            :         }
     498                 :            :         return 0;
     499                 :            : }
     500                 :            : 
     501                 :            : /**
     502                 :            :  * Get bridge MAC addresses.
     503                 :            :  *
     504                 :            :  * @param[in] nlsk_fd
     505                 :            :  *   Netlink socket file descriptor.
     506                 :            :  * @param[in] iface_idx
     507                 :            :  *   Net device interface index.
     508                 :            :  * @param mac[out]
     509                 :            :  *   Pointer to the array table of MAC addresses to fill.
     510                 :            :  *   Its size should be of MLX5_MAX_MAC_ADDRESSES.
     511                 :            :  * @param mac_n[out]
     512                 :            :  *   Number of entries filled in MAC array.
     513                 :            :  *
     514                 :            :  * @return
     515                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     516                 :            :  */
     517                 :            : static int
     518                 :          0 : mlx5_nl_mac_addr_list(int nlsk_fd, unsigned int iface_idx,
     519                 :            :                       struct rte_ether_addr (*mac)[], int *mac_n)
     520                 :            : {
     521                 :            :         struct {
     522                 :            :                 struct nlmsghdr hdr;
     523                 :            :                 struct ifinfomsg ifm;
     524                 :          0 :         } req = {
     525                 :            :                 .hdr = {
     526                 :            :                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
     527                 :            :                         .nlmsg_type = RTM_GETNEIGH,
     528                 :            :                         .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
     529                 :            :                 },
     530                 :            :                 .ifm = {
     531                 :            :                         .ifi_family = PF_BRIDGE,
     532                 :            :                         .ifi_index = iface_idx,
     533                 :            :                 },
     534                 :            :         };
     535                 :          0 :         struct mlx5_nl_mac_addr data = {
     536                 :            :                 .mac = mac,
     537                 :            :                 .mac_n = 0,
     538                 :            :         };
     539                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
     540                 :            :         int ret;
     541                 :            : 
     542         [ #  # ]:          0 :         if (nlsk_fd == -1)
     543                 :            :                 return 0;
     544                 :          0 :         ret = mlx5_nl_request(nlsk_fd, &req.hdr, sn, &req.ifm,
     545                 :            :                               sizeof(struct ifinfomsg));
     546         [ #  # ]:          0 :         if (ret < 0)
     547                 :          0 :                 goto error;
     548                 :          0 :         ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_mac_addr_cb, &data);
     549         [ #  # ]:          0 :         if (ret < 0)
     550                 :          0 :                 goto error;
     551                 :          0 :         *mac_n = data.mac_n;
     552                 :          0 :         return 0;
     553                 :          0 : error:
     554                 :          0 :         DRV_LOG(DEBUG, "Interface %u cannot retrieve MAC address list %s",
     555                 :            :                 iface_idx, strerror(rte_errno));
     556                 :          0 :         return -rte_errno;
     557                 :            : }
     558                 :            : 
     559                 :            : /**
     560                 :            :  * Modify the MAC address neighbour table with Netlink.
     561                 :            :  *
     562                 :            :  * @param[in] nlsk_fd
     563                 :            :  *   Netlink socket file descriptor.
     564                 :            :  * @param[in] iface_idx
     565                 :            :  *   Net device interface index.
     566                 :            :  * @param mac
     567                 :            :  *   MAC address to consider.
     568                 :            :  * @param add
     569                 :            :  *   1 to add the MAC address, 0 to remove the MAC address.
     570                 :            :  *
     571                 :            :  * @return
     572                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     573                 :            :  */
     574                 :            : static int
     575                 :          0 : mlx5_nl_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
     576                 :            :                         struct rte_ether_addr *mac, int add)
     577                 :            : {
     578                 :            :         struct {
     579                 :            :                 struct nlmsghdr hdr;
     580                 :            :                 struct ndmsg ndm;
     581                 :            :                 struct rtattr rta;
     582                 :            :                 uint8_t buffer[RTE_ETHER_ADDR_LEN];
     583         [ #  # ]:          0 :         } req = {
     584                 :            :                 .hdr = {
     585                 :            :                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
     586                 :            :                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
     587                 :            :                                 NLM_F_EXCL | NLM_F_ACK,
     588                 :            :                         .nlmsg_type = add ? RTM_NEWNEIGH : RTM_DELNEIGH,
     589                 :            :                 },
     590                 :            :                 .ndm = {
     591                 :            :                         .ndm_family = PF_BRIDGE,
     592                 :            :                         .ndm_state = NUD_NOARP | NUD_PERMANENT,
     593                 :            :                         .ndm_ifindex = iface_idx,
     594                 :            :                         .ndm_flags = NTF_SELF,
     595                 :            :                 },
     596                 :            :                 .rta = {
     597                 :            :                         .rta_type = NDA_LLADDR,
     598                 :            :                         .rta_len = RTA_LENGTH(RTE_ETHER_ADDR_LEN),
     599                 :            :                 },
     600                 :            :         };
     601                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
     602                 :            :         int ret;
     603                 :            : 
     604         [ #  # ]:          0 :         if (nlsk_fd == -1)
     605                 :            :                 return 0;
     606                 :            :         memcpy(RTA_DATA(&req.rta), mac, RTE_ETHER_ADDR_LEN);
     607                 :          0 :         req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
     608                 :          0 :                 RTA_ALIGN(req.rta.rta_len);
     609                 :          0 :         ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
     610         [ #  # ]:          0 :         if (ret < 0)
     611                 :          0 :                 goto error;
     612                 :          0 :         ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
     613         [ #  # ]:          0 :         if (ret < 0)
     614                 :          0 :                 goto error;
     615                 :            :         return 0;
     616                 :          0 : error:
     617                 :            : #ifdef RTE_LIBRTE_MLX5_DEBUG
     618                 :            :         {
     619                 :            :                 char m[RTE_ETHER_ADDR_FMT_SIZE];
     620                 :            : 
     621                 :            :                 rte_ether_format_addr(m, RTE_ETHER_ADDR_FMT_SIZE, mac);
     622                 :            :                 DRV_LOG(DEBUG,
     623                 :            :                         "Interface %u cannot %s MAC address %s %s",
     624                 :            :                         iface_idx,
     625                 :            :                         add ? "add" : "remove", m, strerror(rte_errno));
     626                 :            :         }
     627                 :            : #endif
     628                 :          0 :         return -rte_errno;
     629                 :            : }
     630                 :            : 
     631                 :            : /**
     632                 :            :  * Modify the VF MAC address neighbour table with Netlink.
     633                 :            :  *
     634                 :            :  * @param[in] nlsk_fd
     635                 :            :  *   Netlink socket file descriptor.
     636                 :            :  * @param[in] iface_idx
     637                 :            :  *   Net device interface index.
     638                 :            :  * @param mac
     639                 :            :  *    MAC address to consider.
     640                 :            :  * @param vf_index
     641                 :            :  *    VF index.
     642                 :            :  *
     643                 :            :  * @return
     644                 :            :  *    0 on success, a negative errno value otherwise and rte_errno is set.
     645                 :            :  */
     646                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_vf_mac_addr_modify)
     647                 :            : int
     648                 :          0 : mlx5_nl_vf_mac_addr_modify(int nlsk_fd, unsigned int iface_idx,
     649                 :            :                            struct rte_ether_addr *mac, int vf_index)
     650                 :            : {
     651                 :            :         int ret;
     652                 :            :         struct {
     653                 :            :                 struct nlmsghdr hdr;
     654                 :            :                 struct ifinfomsg ifm;
     655                 :            :                 struct rtattr vf_list_rta;
     656                 :            :                 struct rtattr vf_info_rta;
     657                 :            :                 struct rtattr vf_mac_rta;
     658                 :            :                 struct ifla_vf_mac ivm;
     659                 :          0 :         } req = {
     660                 :            :                 .hdr = {
     661                 :            :                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
     662                 :            :                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
     663                 :            :                         .nlmsg_type = RTM_BASE,
     664                 :            :                 },
     665                 :            :                 .ifm = {
     666                 :            :                         .ifi_index = iface_idx,
     667                 :            :                 },
     668                 :            :                 .vf_list_rta = {
     669                 :            :                         .rta_type = IFLA_VFINFO_LIST,
     670                 :            :                         .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
     671                 :            :                 },
     672                 :            :                 .vf_info_rta = {
     673                 :            :                         .rta_type = IFLA_VF_INFO,
     674                 :            :                         .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
     675                 :            :                 },
     676                 :            :                 .vf_mac_rta = {
     677                 :            :                         .rta_type = IFLA_VF_MAC,
     678                 :            :                 },
     679                 :            :         };
     680                 :          0 :         struct ifla_vf_mac ivm = {
     681                 :            :                 .vf = vf_index,
     682                 :            :         };
     683         [ #  # ]:          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
     684                 :            : 
     685                 :            :         memcpy(&ivm.mac, mac, RTE_ETHER_ADDR_LEN);
     686                 :            :         memcpy(RTA_DATA(&req.vf_mac_rta), &ivm, sizeof(ivm));
     687                 :            : 
     688                 :          0 :         req.vf_mac_rta.rta_len = RTA_LENGTH(sizeof(ivm));
     689                 :          0 :         req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
     690                 :          0 :                 RTA_ALIGN(req.vf_list_rta.rta_len) +
     691                 :          0 :                 RTA_ALIGN(req.vf_info_rta.rta_len) +
     692                 :            :                 RTA_ALIGN(req.vf_mac_rta.rta_len);
     693                 :          0 :         req.vf_list_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
     694                 :            :                                                &req.vf_list_rta);
     695                 :          0 :         req.vf_info_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
     696                 :            :                                                &req.vf_info_rta);
     697                 :            : 
     698         [ #  # ]:          0 :         if (nlsk_fd < 0)
     699                 :            :                 return -1;
     700                 :          0 :         ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
     701         [ #  # ]:          0 :         if (ret < 0)
     702                 :          0 :                 goto error;
     703                 :          0 :         ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
     704         [ #  # ]:          0 :         if (ret < 0)
     705                 :          0 :                 goto error;
     706                 :            :         return 0;
     707                 :          0 : error:
     708                 :          0 :         DRV_LOG(ERR,
     709                 :            :                 "representor %u cannot set VF MAC address "
     710                 :            :                 RTE_ETHER_ADDR_PRT_FMT " : %s",
     711                 :            :                 vf_index,
     712                 :            :                 RTE_ETHER_ADDR_BYTES(mac),
     713                 :            :                 strerror(rte_errno));
     714                 :          0 :         return -rte_errno;
     715                 :            : }
     716                 :            : 
     717                 :            : /**
     718                 :            :  * Add a MAC address.
     719                 :            :  *
     720                 :            :  * @param[in] nlsk_fd
     721                 :            :  *   Netlink socket file descriptor.
     722                 :            :  * @param[in] iface_idx
     723                 :            :  *   Net device interface index.
     724                 :            :  * @param mac_own
     725                 :            :  *   BITFIELD_DECLARE array to store the mac.
     726                 :            :  * @param mac
     727                 :            :  *   MAC address to register.
     728                 :            :  * @param index
     729                 :            :  *   MAC address index.
     730                 :            :  *
     731                 :            :  * @return
     732                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     733                 :            :  */
     734                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_mac_addr_add)
     735                 :            : int
     736                 :          0 : mlx5_nl_mac_addr_add(int nlsk_fd, unsigned int iface_idx,
     737                 :            :                      uint64_t *mac_own, struct rte_ether_addr *mac,
     738                 :            :                      uint32_t index)
     739                 :            : {
     740                 :            :         int ret;
     741                 :            : 
     742                 :          0 :         ret = mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 1);
     743         [ #  # ]:          0 :         if (!ret) {
     744                 :            :                 MLX5_ASSERT(index < MLX5_MAX_MAC_ADDRESSES);
     745         [ #  # ]:          0 :                 if (index >= MLX5_MAX_MAC_ADDRESSES)
     746                 :            :                         return -EINVAL;
     747                 :            : 
     748                 :          0 :                 BITFIELD_SET(mac_own, index);
     749                 :            :         }
     750         [ #  # ]:          0 :         if (ret == -EEXIST)
     751                 :          0 :                 return 0;
     752                 :            :         return ret;
     753                 :            : }
     754                 :            : 
     755                 :            : /**
     756                 :            :  * Remove a MAC address.
     757                 :            :  *
     758                 :            :  * @param[in] nlsk_fd
     759                 :            :  *   Netlink socket file descriptor.
     760                 :            :  * @param[in] iface_idx
     761                 :            :  *   Net device interface index.
     762                 :            :  * @param mac_own
     763                 :            :  *   BITFIELD_DECLARE array to store the mac.
     764                 :            :  * @param mac
     765                 :            :  *   MAC address to remove.
     766                 :            :  * @param index
     767                 :            :  *   MAC address index.
     768                 :            :  *
     769                 :            :  * @return
     770                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     771                 :            :  */
     772                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_mac_addr_remove)
     773                 :            : int
     774                 :          0 : mlx5_nl_mac_addr_remove(int nlsk_fd, unsigned int iface_idx, uint64_t *mac_own,
     775                 :            :                         struct rte_ether_addr *mac, uint32_t index)
     776                 :            : {
     777                 :            :         MLX5_ASSERT(index < MLX5_MAX_MAC_ADDRESSES);
     778         [ #  # ]:          0 :         if (index >= MLX5_MAX_MAC_ADDRESSES)
     779                 :            :                 return -EINVAL;
     780                 :            : 
     781                 :          0 :         BITFIELD_RESET(mac_own, index);
     782                 :          0 :         return mlx5_nl_mac_addr_modify(nlsk_fd, iface_idx, mac, 0);
     783                 :            : }
     784                 :            : 
     785                 :            : /**
     786                 :            :  * Synchronize Netlink bridge table to the internal table.
     787                 :            :  *
     788                 :            :  * @param[in] nlsk_fd
     789                 :            :  *   Netlink socket file descriptor.
     790                 :            :  * @param[in] iface_idx
     791                 :            :  *   Net device interface index.
     792                 :            :  * @param mac_addrs
     793                 :            :  *   Mac addresses array to sync.
     794                 :            :  * @param n
     795                 :            :  *   @p mac_addrs array size.
     796                 :            :  */
     797                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_mac_addr_sync)
     798                 :            : void
     799                 :          0 : mlx5_nl_mac_addr_sync(int nlsk_fd, unsigned int iface_idx,
     800                 :            :                       struct rte_ether_addr *mac_addrs, int n)
     801                 :          0 : {
     802                 :          0 :         struct rte_ether_addr macs[n];
     803                 :          0 :         int macs_n = 0;
     804                 :            :         int i;
     805                 :            :         int ret;
     806                 :            : 
     807                 :            :         memset(macs, 0, n * sizeof(macs[0]));
     808                 :          0 :         ret = mlx5_nl_mac_addr_list(nlsk_fd, iface_idx, &macs, &macs_n);
     809         [ #  # ]:          0 :         if (ret)
     810                 :          0 :                 return;
     811         [ #  # ]:          0 :         for (i = 0; i != macs_n; ++i) {
     812                 :            :                 int j;
     813                 :            : 
     814                 :            :                 /* Verify the address is not in the array yet. */
     815         [ #  # ]:          0 :                 for (j = 0; j != n; ++j)
     816         [ #  # ]:          0 :                         if (rte_is_same_ether_addr(&macs[i], &mac_addrs[j]))
     817                 :            :                                 break;
     818         [ #  # ]:          0 :                 if (j != n)
     819                 :          0 :                         continue;
     820         [ #  # ]:          0 :                 if (rte_is_multicast_ether_addr(&macs[i])) {
     821                 :            :                         /* Find the first entry available. */
     822         [ #  # ]:          0 :                         for (j = MLX5_MAX_UC_MAC_ADDRESSES; j != n; ++j) {
     823         [ #  # ]:          0 :                                 if (rte_is_zero_ether_addr(&mac_addrs[j])) {
     824                 :          0 :                                         mac_addrs[j] = macs[i];
     825                 :          0 :                                         break;
     826                 :            :                                 }
     827                 :            :                         }
     828                 :            :                 } else {
     829                 :            :                         /* Find the first entry available. */
     830         [ #  # ]:          0 :                         for (j = 0; j != MLX5_MAX_UC_MAC_ADDRESSES; ++j) {
     831         [ #  # ]:          0 :                                 if (rte_is_zero_ether_addr(&mac_addrs[j])) {
     832                 :          0 :                                         mac_addrs[j] = macs[i];
     833                 :          0 :                                         break;
     834                 :            :                                 }
     835                 :            :                         }
     836                 :            :                 }
     837                 :            :         }
     838                 :            : }
     839                 :            : 
     840                 :            : /**
     841                 :            :  * Flush all added MAC addresses.
     842                 :            :  *
     843                 :            :  * @param[in] nlsk_fd
     844                 :            :  *   Netlink socket file descriptor.
     845                 :            :  * @param[in] iface_idx
     846                 :            :  *   Net device interface index.
     847                 :            :  * @param[in] mac_addrs
     848                 :            :  *   Mac addresses array to flush.
     849                 :            :  * @param n
     850                 :            :  *   @p mac_addrs array size.
     851                 :            :  * @param mac_own
     852                 :            :  *   BITFIELD_DECLARE array to store the mac.
     853                 :            :  */
     854                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_mac_addr_flush)
     855                 :            : void
     856                 :          0 : mlx5_nl_mac_addr_flush(int nlsk_fd, unsigned int iface_idx,
     857                 :            :                        struct rte_ether_addr *mac_addrs, int n,
     858                 :            :                        uint64_t *mac_own)
     859                 :            : {
     860                 :            :         int i;
     861                 :            : 
     862         [ #  # ]:          0 :         if (n <= 0 || n > MLX5_MAX_MAC_ADDRESSES)
     863                 :            :                 return;
     864                 :            : 
     865         [ #  # ]:          0 :         for (i = n - 1; i >= 0; --i) {
     866                 :          0 :                 struct rte_ether_addr *m = &mac_addrs[i];
     867                 :            : 
     868         [ #  # ]:          0 :                 if (BITFIELD_ISSET(mac_own, i))
     869                 :          0 :                         mlx5_nl_mac_addr_remove(nlsk_fd, iface_idx, mac_own, m,
     870                 :            :                                                 i);
     871                 :            :         }
     872                 :            : }
     873                 :            : 
     874                 :            : /**
     875                 :            :  * Enable promiscuous / all multicast mode through Netlink.
     876                 :            :  *
     877                 :            :  * @param[in] nlsk_fd
     878                 :            :  *   Netlink socket file descriptor.
     879                 :            :  * @param[in] iface_idx
     880                 :            :  *   Net device interface index.
     881                 :            :  * @param flags
     882                 :            :  *   IFF_PROMISC for promiscuous, IFF_ALLMULTI for allmulti.
     883                 :            :  * @param enable
     884                 :            :  *   Nonzero to enable, disable otherwise.
     885                 :            :  *
     886                 :            :  * @return
     887                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     888                 :            :  */
     889                 :            : static int
     890                 :          0 : mlx5_nl_device_flags(int nlsk_fd, unsigned int iface_idx, uint32_t flags,
     891                 :            :                      int enable)
     892                 :            : {
     893                 :            :         struct {
     894                 :            :                 struct nlmsghdr hdr;
     895                 :            :                 struct ifinfomsg ifi;
     896                 :          0 :         } req = {
     897                 :            :                 .hdr = {
     898                 :            :                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
     899                 :            :                         .nlmsg_type = RTM_NEWLINK,
     900                 :            :                         .nlmsg_flags = NLM_F_REQUEST,
     901                 :            :                 },
     902                 :            :                 .ifi = {
     903         [ #  # ]:          0 :                         .ifi_flags = enable ? flags : 0,
     904                 :            :                         .ifi_change = flags,
     905                 :            :                         .ifi_index = iface_idx,
     906                 :            :                 },
     907                 :            :         };
     908                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
     909                 :            :         int ret;
     910                 :            : 
     911                 :            :         MLX5_ASSERT(!(flags & ~(IFF_PROMISC | IFF_ALLMULTI)));
     912         [ #  # ]:          0 :         if (nlsk_fd < 0)
     913                 :            :                 return 0;
     914                 :          0 :         ret = mlx5_nl_send(nlsk_fd, &req.hdr, sn);
     915                 :            :         if (ret < 0)
     916                 :            :                 return ret;
     917                 :            :         return 0;
     918                 :            : }
     919                 :            : 
     920                 :            : /**
     921                 :            :  * Enable promiscuous mode through Netlink.
     922                 :            :  *
     923                 :            :  * @param[in] nlsk_fd
     924                 :            :  *   Netlink socket file descriptor.
     925                 :            :  * @param[in] iface_idx
     926                 :            :  *   Net device interface index.
     927                 :            :  * @param enable
     928                 :            :  *   Nonzero to enable, disable otherwise.
     929                 :            :  *
     930                 :            :  * @return
     931                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     932                 :            :  */
     933                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_promisc)
     934                 :            : int
     935                 :          0 : mlx5_nl_promisc(int nlsk_fd, unsigned int iface_idx, int enable)
     936                 :            : {
     937                 :          0 :         int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_PROMISC, enable);
     938                 :            : 
     939         [ #  # ]:          0 :         if (ret)
     940         [ #  # ]:          0 :                 DRV_LOG(DEBUG,
     941                 :            :                         "Interface %u cannot %s promisc mode: Netlink error %s",
     942                 :            :                         iface_idx, enable ? "enable" : "disable",
     943                 :            :                         strerror(rte_errno));
     944                 :          0 :         return ret;
     945                 :            : }
     946                 :            : 
     947                 :            : /**
     948                 :            :  * Enable all multicast mode through Netlink.
     949                 :            :  *
     950                 :            :  * @param[in] nlsk_fd
     951                 :            :  *   Netlink socket file descriptor.
     952                 :            :  * @param[in] iface_idx
     953                 :            :  *   Net device interface index.
     954                 :            :  * @param enable
     955                 :            :  *   Nonzero to enable, disable otherwise.
     956                 :            :  *
     957                 :            :  * @return
     958                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     959                 :            :  */
     960                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_allmulti)
     961                 :            : int
     962                 :          0 : mlx5_nl_allmulti(int nlsk_fd, unsigned int iface_idx, int enable)
     963                 :            : {
     964                 :          0 :         int ret = mlx5_nl_device_flags(nlsk_fd, iface_idx, IFF_ALLMULTI,
     965                 :            :                                        enable);
     966                 :            : 
     967         [ #  # ]:          0 :         if (ret)
     968         [ #  # ]:          0 :                 DRV_LOG(DEBUG,
     969                 :            :                         "Interface %u cannot %s allmulti : Netlink error %s",
     970                 :            :                         iface_idx, enable ? "enable" : "disable",
     971                 :            :                         strerror(rte_errno));
     972                 :          0 :         return ret;
     973                 :            : }
     974                 :            : 
     975                 :            : /**
     976                 :            :  * Process network interface information from Netlink message.
     977                 :            :  *
     978                 :            :  * @param nh
     979                 :            :  *   Pointer to Netlink message header.
     980                 :            :  * @param arg
     981                 :            :  *   Opaque data pointer for this callback.
     982                 :            :  *
     983                 :            :  * @return
     984                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     985                 :            :  */
     986                 :            : static int
     987                 :          0 : mlx5_nl_cmdget_cb(struct nlmsghdr *nh, void *arg)
     988                 :            : {
     989                 :            :         struct mlx5_nl_port_info *data = arg;
     990                 :            :         struct mlx5_nl_port_info local = {
     991                 :            :                 .flags = 0,
     992                 :            :         };
     993                 :            :         size_t off = NLMSG_HDRLEN;
     994                 :            : 
     995                 :          0 :         if (nh->nlmsg_type !=
     996         [ #  # ]:          0 :             RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET) &&
     997                 :            :             nh->nlmsg_type !=
     998                 :            :             RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_PORT_GET))
     999                 :          0 :                 goto error;
    1000         [ #  # ]:          0 :         while (off < nh->nlmsg_len) {
    1001                 :          0 :                 struct nlattr *na = (void *)((uintptr_t)nh + off);
    1002                 :          0 :                 void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
    1003                 :            : 
    1004         [ #  # ]:          0 :                 if (na->nla_len > nh->nlmsg_len - off)
    1005                 :          0 :                         goto error;
    1006   [ #  #  #  #  :          0 :                 switch (na->nla_type) {
                   #  # ]
    1007                 :          0 :                 case RDMA_NLDEV_ATTR_DEV_INDEX:
    1008                 :          0 :                         local.ibindex = *(uint32_t *)payload;
    1009                 :          0 :                         local.flags |= MLX5_NL_CMD_GET_IB_INDEX;
    1010                 :          0 :                         break;
    1011                 :          0 :                 case RDMA_NLDEV_ATTR_DEV_NAME:
    1012         [ #  # ]:          0 :                         if (!strcmp(payload, data->name))
    1013                 :          0 :                                 local.flags |= MLX5_NL_CMD_GET_IB_NAME;
    1014                 :            :                         break;
    1015                 :          0 :                 case RDMA_NLDEV_ATTR_NDEV_INDEX:
    1016                 :          0 :                         local.ifindex = *(uint32_t *)payload;
    1017                 :          0 :                         local.flags |= MLX5_NL_CMD_GET_NET_INDEX;
    1018                 :          0 :                         break;
    1019                 :          0 :                 case RDMA_NLDEV_ATTR_PORT_INDEX:
    1020                 :          0 :                         local.portnum = *(uint32_t *)payload;
    1021                 :          0 :                         local.flags |= MLX5_NL_CMD_GET_PORT_INDEX;
    1022                 :          0 :                         break;
    1023                 :          0 :                 case RDMA_NLDEV_ATTR_PORT_STATE:
    1024                 :          0 :                         local.state = *(uint8_t *)payload;
    1025                 :          0 :                         local.flags |= MLX5_NL_CMD_GET_PORT_STATE;
    1026                 :          0 :                         break;
    1027                 :            :                 default:
    1028                 :            :                         break;
    1029                 :            :                 }
    1030                 :          0 :                 off += NLA_ALIGN(na->nla_len);
    1031                 :            :         }
    1032                 :            :         /*
    1033                 :            :          * It is possible to have multiple messages for all
    1034                 :            :          * Infiniband devices in the system with appropriate name.
    1035                 :            :          * So we should gather parameters locally and copy to
    1036                 :            :          * query context only in case of coinciding device name.
    1037                 :            :          */
    1038         [ #  # ]:          0 :         if (local.flags & MLX5_NL_CMD_GET_IB_NAME) {
    1039                 :          0 :                 data->flags = local.flags;
    1040                 :          0 :                 data->ibindex = local.ibindex;
    1041                 :          0 :                 data->ifindex = local.ifindex;
    1042                 :          0 :                 data->portnum = local.portnum;
    1043                 :          0 :                 data->state = local.state;
    1044                 :            :         }
    1045                 :            :         return 0;
    1046                 :          0 : error:
    1047                 :          0 :         rte_errno = EINVAL;
    1048                 :          0 :         return -rte_errno;
    1049                 :            : }
    1050                 :            : 
    1051                 :            : /**
    1052                 :            :  * Get port info of network interface associated with some IB device.
    1053                 :            :  *
    1054                 :            :  * This is the only somewhat safe method to avoid resorting to heuristics
    1055                 :            :  * when faced with port representors. Unfortunately it requires at least
    1056                 :            :  * Linux 4.17.
    1057                 :            :  *
    1058                 :            :  * @param nl
    1059                 :            :  *   Netlink socket of the RDMA kind (NETLINK_RDMA).
    1060                 :            :  * @param[in] pindex
    1061                 :            :  *   IB device port index, starting from 1
    1062                 :            :  * @param[out] data
    1063                 :            :  *   Pointer to port info.
    1064                 :            :  * @return
    1065                 :            :  *   0 on success, negative on error and rte_errno is set.
    1066                 :            :  */
    1067                 :            : static int
    1068                 :          0 : mlx5_nl_port_info(int nl, uint32_t pindex, struct mlx5_nl_port_info *data)
    1069                 :            : {
    1070                 :            :         union {
    1071                 :            :                 struct nlmsghdr nh;
    1072                 :            :                 uint8_t buf[NLMSG_HDRLEN +
    1073                 :            :                             NLA_HDRLEN + NLA_ALIGN(sizeof(data->ibindex)) +
    1074                 :            :                             NLA_HDRLEN + NLA_ALIGN(sizeof(pindex))];
    1075                 :          0 :         } req = {
    1076                 :            :                 .nh = {
    1077                 :            :                         .nlmsg_len = NLMSG_LENGTH(0),
    1078                 :            :                         .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
    1079                 :            :                                                        RDMA_NLDEV_CMD_GET),
    1080                 :            :                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
    1081                 :            :                 },
    1082                 :            :         };
    1083                 :            :         struct nlattr *na;
    1084                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    1085                 :            :         int ret;
    1086                 :            : 
    1087         [ #  # ]:          0 :         if (data->ibindex == UINT32_MAX) {
    1088                 :          0 :                 ret = mlx5_nl_send(nl, &req.nh, sn);
    1089         [ #  # ]:          0 :                 if (ret < 0)
    1090                 :            :                         return ret;
    1091                 :          0 :                 ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, data);
    1092         [ #  # ]:          0 :                 if (ret < 0)
    1093                 :            :                         return ret;
    1094         [ #  # ]:          0 :                 if (!(data->flags & MLX5_NL_CMD_GET_IB_NAME) ||
    1095                 :            :                     !(data->flags & MLX5_NL_CMD_GET_IB_INDEX))
    1096                 :          0 :                         goto error;
    1097                 :          0 :                 data->flags = 0;
    1098                 :            :         }
    1099                 :          0 :         sn = MLX5_NL_SN_GENERATE;
    1100                 :          0 :         req.nh.nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
    1101                 :            :                                              RDMA_NLDEV_CMD_PORT_GET);
    1102                 :          0 :         req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
    1103                 :          0 :         req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.buf) - NLMSG_HDRLEN);
    1104                 :          0 :         na = (void *)((uintptr_t)req.buf + NLMSG_HDRLEN);
    1105                 :          0 :         na->nla_len = NLA_HDRLEN + sizeof(data->ibindex);
    1106                 :          0 :         na->nla_type = RDMA_NLDEV_ATTR_DEV_INDEX;
    1107                 :          0 :         memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
    1108                 :          0 :                &data->ibindex, sizeof(data->ibindex));
    1109                 :          0 :         na = (void *)((uintptr_t)na + NLA_ALIGN(na->nla_len));
    1110                 :          0 :         na->nla_len = NLA_HDRLEN + sizeof(pindex);
    1111                 :          0 :         na->nla_type = RDMA_NLDEV_ATTR_PORT_INDEX;
    1112                 :          0 :         memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
    1113                 :            :                &pindex, sizeof(pindex));
    1114                 :          0 :         ret = mlx5_nl_send(nl, &req.nh, sn);
    1115         [ #  # ]:          0 :         if (ret < 0)
    1116                 :            :                 return ret;
    1117                 :          0 :         ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, data);
    1118         [ #  # ]:          0 :         if (ret < 0)
    1119                 :            :                 return ret;
    1120                 :          0 :         if (!(data->flags & MLX5_NL_CMD_GET_IB_NAME) ||
    1121         [ #  # ]:          0 :             !(data->flags & MLX5_NL_CMD_GET_IB_INDEX) ||
    1122                 :          0 :             !(data->flags & MLX5_NL_CMD_GET_NET_INDEX) ||
    1123         [ #  # ]:          0 :             !data->ifindex)
    1124                 :          0 :                 goto error;
    1125                 :            :         return 0;
    1126                 :          0 : error:
    1127                 :          0 :         rte_errno = ENODEV;
    1128                 :          0 :         return -rte_errno;
    1129                 :            : }
    1130                 :            : 
    1131                 :            : /**
    1132                 :            :  * Get index of network interface associated with some IB device.
    1133                 :            :  *
    1134                 :            :  * This is the only somewhat safe method to avoid resorting to heuristics
    1135                 :            :  * when faced with port representors. Unfortunately it requires at least
    1136                 :            :  * Linux 4.17.
    1137                 :            :  *
    1138                 :            :  * @param nl
    1139                 :            :  *   Netlink socket of the RDMA kind (NETLINK_RDMA).
    1140                 :            :  * @param[in] name
    1141                 :            :  *   IB device name.
    1142                 :            :  * @param[in] pindex
    1143                 :            :  *   IB device port index, starting from 1
    1144                 :            :  * @param[in] dev_info
    1145                 :            :  *   Cached mlx5 device information.
    1146                 :            :  * @return
    1147                 :            :  *   A valid (nonzero) interface index on success, 0 otherwise and rte_errno
    1148                 :            :  *   is set.
    1149                 :            :  */
    1150                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_ifindex)
    1151                 :            : unsigned int
    1152                 :          0 : mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex, struct mlx5_dev_info *dev_info)
    1153                 :            : {
    1154                 :            :         int ret;
    1155                 :            : 
    1156                 :          0 :         struct mlx5_nl_port_info data = {
    1157                 :            :                         .ifindex = 0,
    1158                 :            :                         .name = name,
    1159                 :            :                         .ibindex = UINT32_MAX,
    1160                 :            :                         .flags = 0,
    1161                 :            :         };
    1162                 :            : 
    1163   [ #  #  #  # ]:          0 :         if (dev_info->probe_opt && !strcmp(name, dev_info->ibname)) {
    1164   [ #  #  #  # ]:          0 :                 if (dev_info->port_info && pindex <= dev_info->port_num &&
    1165         [ #  # ]:          0 :                     dev_info->port_info[pindex].valid) {
    1166         [ #  # ]:          0 :                         if (!dev_info->port_info[pindex].ifindex)
    1167                 :          0 :                                 rte_errno = ENODEV;
    1168                 :          0 :                         return dev_info->port_info[pindex].ifindex;
    1169                 :            :                 }
    1170         [ #  # ]:          0 :                 if (dev_info->port_num)
    1171                 :          0 :                         data.ibindex = dev_info->ibindex;
    1172                 :            :         }
    1173                 :            : 
    1174                 :          0 :         ret = mlx5_nl_port_info(nl, pindex, &data);
    1175                 :            : 
    1176   [ #  #  #  # ]:          0 :         if (dev_info->probe_opt && !strcmp(dev_info->ibname, name)) {
    1177   [ #  #  #  # ]:          0 :                 if ((!ret || ret == -ENODEV) && dev_info->port_info &&
    1178         [ #  # ]:          0 :                     pindex <= dev_info->port_num) {
    1179         [ #  # ]:          0 :                         if (!ret)
    1180                 :          0 :                                 dev_info->port_info[pindex].ifindex = data.ifindex;
    1181                 :            :                         /* -ENODEV means the pindex is unused but still valid case */
    1182                 :          0 :                         dev_info->port_info[pindex].valid = 1;
    1183                 :            :                 }
    1184                 :            :         }
    1185                 :            : 
    1186         [ #  # ]:          0 :         return ret ? 0 : data.ifindex;
    1187                 :            : }
    1188                 :            : 
    1189                 :            : /**
    1190                 :            :  * Get IB device port state.
    1191                 :            :  *
    1192                 :            :  * This is the only somewhat safe method to get info for port number >= 255.
    1193                 :            :  * Unfortunately it requires at least Linux 4.17.
    1194                 :            :  *
    1195                 :            :  * @param nl
    1196                 :            :  *   Netlink socket of the RDMA kind (NETLINK_RDMA).
    1197                 :            :  * @param[in] name
    1198                 :            :  *   IB device name.
    1199                 :            :  * @param[in] pindex
    1200                 :            :  *   IB device port index, starting from 1
    1201                 :            :  * @param[in] dev_info
    1202                 :            :  *   Cached mlx5 device information.
    1203                 :            :  * @return
    1204                 :            :  *   Port state (ibv_port_state) on success, negative on error
    1205                 :            :  *   and rte_errno is set.
    1206                 :            :  */
    1207                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_port_state)
    1208                 :            : int
    1209                 :          0 : mlx5_nl_port_state(int nl, const char *name, uint32_t pindex, struct mlx5_dev_info *dev_info)
    1210                 :            : {
    1211                 :          0 :         struct mlx5_nl_port_info data = {
    1212                 :            :                         .state = 0,
    1213                 :            :                         .name = name,
    1214                 :            :                         .ibindex = UINT32_MAX,
    1215                 :            :         };
    1216                 :            : 
    1217   [ #  #  #  # ]:          0 :         if (dev_info && dev_info->probe_opt &&
    1218   [ #  #  #  # ]:          0 :             !strcmp(name, dev_info->ibname) && dev_info->port_num)
    1219                 :          0 :                 data.ibindex = dev_info->ibindex;
    1220         [ #  # ]:          0 :         if (mlx5_nl_port_info(nl, pindex, &data) < 0)
    1221                 :          0 :                 return -rte_errno;
    1222         [ #  # ]:          0 :         if ((data.flags & MLX5_NL_CMD_GET_PORT_STATE) == 0) {
    1223                 :          0 :                 rte_errno = ENOTSUP;
    1224                 :          0 :                 return -rte_errno;
    1225                 :            :         }
    1226                 :          0 :         return (int)data.state;
    1227                 :            : }
    1228                 :            : 
    1229                 :            : /**
    1230                 :            :  * Get the number of physical ports of given IB device.
    1231                 :            :  *
    1232                 :            :  * @param nl
    1233                 :            :  *   Netlink socket of the RDMA kind (NETLINK_RDMA).
    1234                 :            :  * @param[in] name
    1235                 :            :  *   IB device name.
    1236                 :            :  * @param[in] dev_info
    1237                 :            :  *   Cached mlx5 device info.
    1238                 :            :  *
    1239                 :            :  * @return
    1240                 :            :  *   A valid (nonzero) number of ports on success, 0 otherwise
    1241                 :            :  *   and rte_errno is set.
    1242                 :            :  */
    1243                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_portnum)
    1244                 :            : unsigned int
    1245                 :          0 : mlx5_nl_portnum(int nl, const char *name, struct mlx5_dev_info *dev_info)
    1246                 :            : {
    1247                 :          0 :         struct mlx5_nl_port_info data = {
    1248                 :            :                 .flags = 0,
    1249                 :            :                 .name = name,
    1250                 :            :                 .ifindex = 0,
    1251                 :            :                 .portnum = 0,
    1252                 :            :         };
    1253                 :          0 :         struct nlmsghdr req = {
    1254                 :            :                 .nlmsg_len = NLMSG_LENGTH(0),
    1255                 :            :                 .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
    1256                 :            :                                                RDMA_NLDEV_CMD_GET),
    1257                 :            :                 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
    1258                 :            :         };
    1259                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    1260                 :            :         int ret, size;
    1261                 :            : 
    1262   [ #  #  #  # ]:          0 :         if (dev_info->probe_opt && dev_info->port_num &&
    1263         [ #  # ]:          0 :             !strcmp(name, dev_info->ibname))
    1264                 :            :                 return dev_info->port_num;
    1265                 :            : 
    1266                 :          0 :         ret = mlx5_nl_send(nl, &req, sn);
    1267         [ #  # ]:          0 :         if (ret < 0)
    1268                 :            :                 return 0;
    1269                 :          0 :         ret = mlx5_nl_recv(nl, sn, mlx5_nl_cmdget_cb, &data);
    1270         [ #  # ]:          0 :         if (ret < 0)
    1271                 :            :                 return 0;
    1272                 :          0 :         if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
    1273         [ #  # ]:          0 :             !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
    1274                 :            :             !(data.flags & MLX5_NL_CMD_GET_PORT_INDEX)) {
    1275                 :          0 :                 rte_errno = ENODEV;
    1276                 :          0 :                 return 0;
    1277                 :            :         }
    1278         [ #  # ]:          0 :         if (!data.portnum) {
    1279                 :          0 :                 rte_errno = EINVAL;
    1280                 :          0 :                 return 0;
    1281                 :            :         }
    1282         [ #  # ]:          0 :         if (!dev_info->probe_opt)
    1283                 :            :                 return data.portnum;
    1284                 :            :         MLX5_ASSERT(!strlen(dev_info->ibname));
    1285                 :          0 :         dev_info->port_num = data.portnum;
    1286                 :          0 :         dev_info->ibindex = data.ibindex;
    1287         [ #  # ]:          0 :         snprintf(dev_info->ibname, MLX5_FS_NAME_MAX, "%s", name);
    1288         [ #  # ]:          0 :         if (data.portnum > 1) {
    1289                 :          0 :                 size = (data.portnum + 1) * sizeof(struct mlx5_port_nl_info);
    1290                 :          0 :                 dev_info->port_info = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE, size,
    1291                 :            :                                                   RTE_CACHE_LINE_SIZE,
    1292                 :            :                                                   SOCKET_ID_ANY);
    1293         [ #  # ]:          0 :                 if (dev_info->port_info == NULL) {
    1294                 :            :                         memset(dev_info, 0, sizeof(*dev_info));
    1295                 :          0 :                         rte_errno = ENOMEM;
    1296                 :          0 :                         return 0;
    1297                 :            :                 }
    1298                 :            :         }
    1299                 :          0 :         return data.portnum;
    1300                 :            : }
    1301                 :            : 
    1302                 :            : /**
    1303                 :            :  * Analyze gathered port parameters via Netlink to recognize master
    1304                 :            :  * and representor devices for E-Switch configuration.
    1305                 :            :  *
    1306                 :            :  * @param[in] num_vf_set
    1307                 :            :  *   flag of presence of number of VFs port attribute.
    1308                 :            :  * @param[inout] switch_info
    1309                 :            :  *   Port information, including port name as a number and port name
    1310                 :            :  *   type if recognized
    1311                 :            :  *
    1312                 :            :  * @return
    1313                 :            :  *   master and representor flags are set in switch_info according to
    1314                 :            :  *   recognized parameters (if any).
    1315                 :            :  */
    1316                 :            : static void
    1317                 :            : mlx5_nl_check_switch_info(bool num_vf_set,
    1318                 :            :                           struct mlx5_switch_info *switch_info)
    1319                 :            : {
    1320   [ #  #  #  #  :          0 :         switch (switch_info->name_type) {
                   #  # ]
    1321                 :          0 :         case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
    1322                 :            :                 /*
    1323                 :            :                  * Name is not recognized, assume the master,
    1324                 :            :                  * check the number of VFs key presence.
    1325                 :            :                  */
    1326                 :          0 :                 switch_info->master = num_vf_set;
    1327                 :          0 :                 break;
    1328                 :          0 :         case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
    1329                 :            :                 /*
    1330                 :            :                  * Name is not set, this assumes the legacy naming
    1331                 :            :                  * schema for master, just check if there is a
    1332                 :            :                  * number of VFs key.
    1333                 :            :                  */
    1334                 :          0 :                 switch_info->master = num_vf_set;
    1335                 :          0 :                 break;
    1336                 :          0 :         case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
    1337                 :            :                 /* New uplink naming schema recognized. */
    1338                 :          0 :                 switch_info->master = 1;
    1339                 :          0 :                 break;
    1340                 :          0 :         case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
    1341                 :            :                 /* Legacy representors naming schema. */
    1342                 :          0 :                 switch_info->representor = !num_vf_set;
    1343                 :          0 :                 break;
    1344                 :          0 :         case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
    1345                 :            :                 /* Fallthrough */
    1346                 :            :         case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
    1347                 :            :                 /* Fallthrough */
    1348                 :            :         case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
    1349                 :            :                 /* New representors naming schema. */
    1350                 :          0 :                 switch_info->representor = 1;
    1351                 :          0 :                 break;
    1352                 :            :         }
    1353                 :            : }
    1354                 :            : 
    1355                 :            : /**
    1356                 :            :  * Process switch information from Netlink message.
    1357                 :            :  *
    1358                 :            :  * @param nh
    1359                 :            :  *   Pointer to Netlink message header.
    1360                 :            :  * @param arg
    1361                 :            :  *   Opaque data pointer for this callback.
    1362                 :            :  *
    1363                 :            :  * @return
    1364                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1365                 :            :  */
    1366                 :            : static int
    1367                 :          0 : mlx5_nl_switch_info_cb(struct nlmsghdr *nh, void *arg)
    1368                 :            : {
    1369                 :          0 :         struct mlx5_switch_info info = {
    1370                 :            :                 .master = 0,
    1371                 :            :                 .representor = 0,
    1372                 :            :                 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
    1373                 :            :                 .port_name = 0,
    1374                 :            :                 .switch_id = 0,
    1375                 :            :         };
    1376                 :            :         size_t off = NLMSG_LENGTH(sizeof(struct ifinfomsg));
    1377                 :            :         bool switch_id_set = false;
    1378                 :            :         bool num_vf_set = false;
    1379                 :            :         int len;
    1380                 :            : 
    1381         [ #  # ]:          0 :         if (nh->nlmsg_type != RTM_NEWLINK)
    1382                 :          0 :                 goto error;
    1383         [ #  # ]:          0 :         while (off < nh->nlmsg_len) {
    1384                 :          0 :                 struct rtattr *ra = (void *)((uintptr_t)nh + off);
    1385                 :          0 :                 void *payload = RTA_DATA(ra);
    1386                 :            :                 unsigned int i;
    1387                 :            : 
    1388         [ #  # ]:          0 :                 if (ra->rta_len > nh->nlmsg_len - off)
    1389                 :          0 :                         goto error;
    1390   [ #  #  #  # ]:          0 :                 switch (ra->rta_type) {
    1391                 :          0 :                 case IFLA_NUM_VF:
    1392                 :            :                         num_vf_set = true;
    1393                 :          0 :                         break;
    1394                 :          0 :                 case IFLA_PHYS_PORT_NAME:
    1395                 :          0 :                         len = RTA_PAYLOAD(ra);
    1396                 :            :                         /* Some kernels do not pad attributes with zero. */
    1397         [ #  # ]:          0 :                         if (len > 0 && len < MLX5_PHYS_PORT_NAME_MAX) {
    1398                 :            :                                 char name[MLX5_PHYS_PORT_NAME_MAX];
    1399                 :            : 
    1400                 :            :                                 /*
    1401                 :            :                                  * We can't just patch the message with padding
    1402                 :            :                                  * zero - it might corrupt the following items
    1403                 :            :                                  * in the message, we have to copy the string
    1404                 :            :                                  * by attribute length and pad the copied one.
    1405                 :            :                                  */
    1406                 :          0 :                                 memcpy(name, payload, len);
    1407                 :          0 :                                 name[len] = 0;
    1408                 :          0 :                                 mlx5_translate_port_name(name, &info);
    1409                 :            :                         } else {
    1410                 :          0 :                                 info.name_type =
    1411                 :            :                                         MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
    1412                 :            :                         }
    1413                 :            :                         break;
    1414                 :          0 :                 case IFLA_PHYS_SWITCH_ID:
    1415                 :          0 :                         info.switch_id = 0;
    1416         [ #  # ]:          0 :                         for (i = 0; i < RTA_PAYLOAD(ra); ++i) {
    1417                 :          0 :                                 info.switch_id <<= 8;
    1418                 :          0 :                                 info.switch_id |= ((uint8_t *)payload)[i];
    1419                 :            :                         }
    1420                 :            :                         switch_id_set = true;
    1421                 :            :                         break;
    1422                 :            :                 }
    1423                 :          0 :                 off += RTA_ALIGN(ra->rta_len);
    1424                 :            :         }
    1425         [ #  # ]:          0 :         if (switch_id_set) {
    1426                 :            :                 /* We have some E-Switch configuration. */
    1427                 :            :                 mlx5_nl_check_switch_info(num_vf_set, &info);
    1428                 :            :         }
    1429                 :            :         MLX5_ASSERT(!(info.master && info.representor));
    1430                 :            :         memcpy(arg, &info, sizeof(info));
    1431                 :          0 :         return 0;
    1432                 :          0 : error:
    1433                 :          0 :         rte_errno = EINVAL;
    1434                 :          0 :         return -rte_errno;
    1435                 :            : }
    1436                 :            : 
    1437                 :            : /**
    1438                 :            :  * Get switch information associated with network interface.
    1439                 :            :  *
    1440                 :            :  * @param nl
    1441                 :            :  *   Netlink socket of the ROUTE kind (NETLINK_ROUTE).
    1442                 :            :  * @param ifindex
    1443                 :            :  *   Network interface index.
    1444                 :            :  * @param[out] info
    1445                 :            :  *   Switch information object, populated in case of success.
    1446                 :            :  *
    1447                 :            :  * @return
    1448                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1449                 :            :  */
    1450                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_switch_info)
    1451                 :            : int
    1452                 :          0 : mlx5_nl_switch_info(int nl, unsigned int ifindex,
    1453                 :            :                     struct mlx5_switch_info *info)
    1454                 :            : {
    1455                 :            :         struct {
    1456                 :            :                 struct nlmsghdr nh;
    1457                 :            :                 struct ifinfomsg info;
    1458                 :            :                 struct rtattr rta;
    1459                 :            :                 uint32_t extmask;
    1460                 :          0 :         } req = {
    1461                 :            :                 .nh = {
    1462                 :            :                         .nlmsg_len = NLMSG_LENGTH
    1463                 :            :                                         (sizeof(req.info) +
    1464                 :            :                                          RTA_LENGTH(sizeof(uint32_t))),
    1465                 :            :                         .nlmsg_type = RTM_GETLINK,
    1466                 :            :                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
    1467                 :            :                 },
    1468                 :            :                 .info = {
    1469                 :            :                         .ifi_family = AF_UNSPEC,
    1470                 :            :                         .ifi_index = ifindex,
    1471                 :            :                 },
    1472                 :            :                 .rta = {
    1473                 :            :                         .rta_type = IFLA_EXT_MASK,
    1474                 :            :                         .rta_len = RTA_LENGTH(sizeof(int32_t)),
    1475                 :            :                 },
    1476                 :            :                 .extmask = RTE_LE32(1),
    1477                 :            :         };
    1478                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    1479                 :            :         int ret;
    1480                 :            : 
    1481                 :          0 :         ret = mlx5_nl_send(nl, &req.nh, sn);
    1482         [ #  # ]:          0 :         if (ret >= 0)
    1483                 :          0 :                 ret = mlx5_nl_recv(nl, sn, mlx5_nl_switch_info_cb, info);
    1484         [ #  # ]:          0 :         if (info->master && info->representor) {
    1485                 :          0 :                 DRV_LOG(ERR, "ifindex %u device is recognized as master"
    1486                 :            :                              " and as representor", ifindex);
    1487                 :          0 :                 rte_errno = ENODEV;
    1488                 :            :                 ret = -rte_errno;
    1489                 :            :         }
    1490                 :          0 :         return ret;
    1491                 :            : }
    1492                 :            : 
    1493                 :            : /*
    1494                 :            :  * Delete VLAN network device by ifindex.
    1495                 :            :  *
    1496                 :            :  * @param[in] tcf
    1497                 :            :  *   Context object initialized by mlx5_nl_vlan_vmwa_init().
    1498                 :            :  * @param[in] ifindex
    1499                 :            :  *   Interface index of network device to delete.
    1500                 :            :  */
    1501                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_vlan_vmwa_delete)
    1502                 :            : void
    1503                 :          0 : mlx5_nl_vlan_vmwa_delete(struct mlx5_nl_vlan_vmwa_context *vmwa,
    1504                 :            :                       uint32_t ifindex)
    1505                 :            : {
    1506                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    1507                 :            :         int ret;
    1508                 :            :         struct {
    1509                 :            :                 struct nlmsghdr nh;
    1510                 :            :                 struct ifinfomsg info;
    1511                 :          0 :         } req = {
    1512                 :            :                 .nh = {
    1513                 :            :                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
    1514                 :            :                         .nlmsg_type = RTM_DELLINK,
    1515                 :            :                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
    1516                 :            :                 },
    1517                 :            :                 .info = {
    1518                 :            :                         .ifi_family = AF_UNSPEC,
    1519                 :            :                         .ifi_index = ifindex,
    1520                 :            :                 },
    1521                 :            :         };
    1522                 :            : 
    1523         [ #  # ]:          0 :         if (ifindex) {
    1524                 :          0 :                 ret = mlx5_nl_send(vmwa->nl_socket, &req.nh, sn);
    1525         [ #  # ]:          0 :                 if (ret >= 0)
    1526                 :          0 :                         ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
    1527         [ #  # ]:          0 :                 if (ret < 0)
    1528                 :          0 :                         DRV_LOG(WARNING, "netlink: error deleting VLAN WA"
    1529                 :            :                                 " ifindex %u, %d", ifindex, ret);
    1530                 :            :         }
    1531                 :          0 : }
    1532                 :            : 
    1533                 :            : /* Set of subroutines to build Netlink message. */
    1534                 :            : static struct nlattr *
    1535                 :            : nl_msg_tail(struct nlmsghdr *nlh)
    1536                 :            : {
    1537                 :          0 :         return (struct nlattr *)
    1538                 :          0 :                 (((uint8_t *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
    1539                 :            : }
    1540                 :            : 
    1541                 :            : static void
    1542                 :          0 : nl_attr_put(struct nlmsghdr *nlh, int type, const void *data, int alen)
    1543                 :            : {
    1544                 :            :         struct nlattr *nla = nl_msg_tail(nlh);
    1545                 :            : 
    1546                 :          0 :         nla->nla_type = type;
    1547                 :          0 :         nla->nla_len = NLMSG_ALIGN(sizeof(struct nlattr)) + alen;
    1548                 :          0 :         nlh->nlmsg_len += NLMSG_ALIGN(nla->nla_len);
    1549                 :            : 
    1550         [ #  # ]:          0 :         if (alen)
    1551                 :          0 :                 memcpy((uint8_t *)nla + sizeof(struct nlattr), data, alen);
    1552                 :          0 : }
    1553                 :            : 
    1554                 :            : static struct nlattr *
    1555                 :            : nl_attr_nest_start(struct nlmsghdr *nlh, int type)
    1556                 :            : {
    1557                 :            :         struct nlattr *nest = (struct nlattr *)nl_msg_tail(nlh);
    1558                 :            : 
    1559                 :            :         nl_attr_put(nlh, type, NULL, 0);
    1560                 :            :         return nest;
    1561                 :            : }
    1562                 :            : 
    1563                 :            : static void
    1564                 :            : nl_attr_nest_end(struct nlmsghdr *nlh, struct nlattr *nest)
    1565                 :            : {
    1566                 :          0 :         nest->nla_len = (uint8_t *)nl_msg_tail(nlh) - (uint8_t *)nest;
    1567                 :            : }
    1568                 :            : 
    1569                 :            : /*
    1570                 :            :  * Create network VLAN device with specified VLAN tag.
    1571                 :            :  *
    1572                 :            :  * @param[in] tcf
    1573                 :            :  *   Context object initialized by mlx5_nl_vlan_vmwa_init().
    1574                 :            :  * @param[in] ifindex
    1575                 :            :  *   Base network interface index.
    1576                 :            :  * @param[in] tag
    1577                 :            :  *   VLAN tag for VLAN network device to create.
    1578                 :            :  */
    1579                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_vlan_vmwa_create)
    1580                 :            : uint32_t
    1581                 :          0 : mlx5_nl_vlan_vmwa_create(struct mlx5_nl_vlan_vmwa_context *vmwa,
    1582                 :            :                          uint32_t ifindex, uint16_t tag)
    1583                 :            : {
    1584                 :            :         struct nlmsghdr *nlh;
    1585                 :            :         struct ifinfomsg *ifm;
    1586                 :            :         char name[sizeof(MLX5_VMWA_VLAN_DEVICE_PFX) + 32];
    1587                 :            : 
    1588                 :            :         alignas(RTE_CACHE_LINE_SIZE)
    1589                 :            :         uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
    1590                 :            :                     NLMSG_ALIGN(sizeof(struct ifinfomsg)) +
    1591                 :            :                     NLMSG_ALIGN(sizeof(struct nlattr)) * 8 +
    1592                 :            :                     NLMSG_ALIGN(sizeof(uint32_t)) +
    1593                 :            :                     NLMSG_ALIGN(sizeof(name)) +
    1594                 :            :                     NLMSG_ALIGN(sizeof("vlan")) +
    1595                 :            :                     NLMSG_ALIGN(sizeof(uint32_t)) +
    1596                 :            :                     NLMSG_ALIGN(sizeof(uint16_t)) + 16];
    1597                 :            :         struct nlattr *na_info;
    1598                 :            :         struct nlattr *na_vlan;
    1599                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    1600                 :            :         int ret;
    1601                 :            : 
    1602                 :            :         memset(buf, 0, sizeof(buf));
    1603                 :            :         nlh = (struct nlmsghdr *)buf;
    1604                 :            :         nlh->nlmsg_len = sizeof(struct nlmsghdr);
    1605                 :          0 :         nlh->nlmsg_type = RTM_NEWLINK;
    1606                 :          0 :         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
    1607                 :            :                            NLM_F_EXCL | NLM_F_ACK;
    1608                 :            :         ifm = (struct ifinfomsg *)nl_msg_tail(nlh);
    1609                 :          0 :         nlh->nlmsg_len += sizeof(struct ifinfomsg);
    1610                 :            :         ifm->ifi_family = AF_UNSPEC;
    1611                 :            :         ifm->ifi_type = 0;
    1612                 :            :         ifm->ifi_index = 0;
    1613                 :          0 :         ifm->ifi_flags = IFF_UP;
    1614                 :          0 :         ifm->ifi_change = 0xffffffff;
    1615                 :          0 :         nl_attr_put(nlh, IFLA_LINK, &ifindex, sizeof(ifindex));
    1616                 :          0 :         ret = snprintf(name, sizeof(name), "%s.%u.%u",
    1617                 :            :                        MLX5_VMWA_VLAN_DEVICE_PFX, ifindex, tag);
    1618                 :          0 :         nl_attr_put(nlh, IFLA_IFNAME, name, ret + 1);
    1619                 :            :         na_info = nl_attr_nest_start(nlh, IFLA_LINKINFO);
    1620                 :          0 :         nl_attr_put(nlh, IFLA_INFO_KIND, "vlan", sizeof("vlan"));
    1621                 :            :         na_vlan = nl_attr_nest_start(nlh, IFLA_INFO_DATA);
    1622                 :          0 :         nl_attr_put(nlh, IFLA_VLAN_ID, &tag, sizeof(tag));
    1623                 :            :         nl_attr_nest_end(nlh, na_vlan);
    1624                 :            :         nl_attr_nest_end(nlh, na_info);
    1625                 :            :         MLX5_ASSERT(sizeof(buf) >= nlh->nlmsg_len);
    1626                 :          0 :         ret = mlx5_nl_send(vmwa->nl_socket, nlh, sn);
    1627         [ #  # ]:          0 :         if (ret >= 0)
    1628                 :          0 :                 ret = mlx5_nl_recv(vmwa->nl_socket, sn, NULL, NULL);
    1629         [ #  # ]:          0 :         if (ret < 0) {
    1630                 :          0 :                 DRV_LOG(WARNING, "netlink: VLAN %s create failure (%d)", name,
    1631                 :            :                         ret);
    1632                 :            :         }
    1633                 :            :         /* Try to get ifindex of created or pre-existing device. */
    1634                 :          0 :         ret = if_nametoindex(name);
    1635         [ #  # ]:          0 :         if (!ret) {
    1636                 :          0 :                 DRV_LOG(WARNING, "VLAN %s failed to get index (%d)", name,
    1637                 :            :                         errno);
    1638                 :          0 :                 return 0;
    1639                 :            :         }
    1640                 :            :         return ret;
    1641                 :            : }
    1642                 :            : 
    1643                 :            : /**
    1644                 :            :  * Parse Netlink message to retrieve the general family ID.
    1645                 :            :  *
    1646                 :            :  * @param nh
    1647                 :            :  *   Pointer to Netlink Message Header.
    1648                 :            :  * @param arg
    1649                 :            :  *   PMD data register with this callback.
    1650                 :            :  *
    1651                 :            :  * @return
    1652                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1653                 :            :  */
    1654                 :            : static int
    1655                 :          0 : mlx5_nl_family_id_cb(struct nlmsghdr *nh, void *arg)
    1656                 :            : {
    1657                 :            : 
    1658                 :          0 :         struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
    1659                 :          0 :         struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
    1660                 :            :                                         NLMSG_ALIGN(sizeof(struct genlmsghdr)));
    1661                 :            : 
    1662   [ #  #  #  # ]:          0 :         for (; nla->nla_len && nla < tail;
    1663                 :          0 :              nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len))) {
    1664         [ #  # ]:          0 :                 if (nla->nla_type == CTRL_ATTR_FAMILY_ID) {
    1665                 :          0 :                         *(uint16_t *)arg = *(uint16_t *)(nla + 1);
    1666                 :          0 :                         return 0;
    1667                 :            :                 }
    1668                 :            :         }
    1669                 :            :         return -EINVAL;
    1670                 :            : }
    1671                 :            : 
    1672                 :            : #define MLX5_NL_MAX_ATTR_SIZE 100
    1673                 :            : /**
    1674                 :            :  * Get generic netlink family ID.
    1675                 :            :  *
    1676                 :            :  * @param[in] nlsk_fd
    1677                 :            :  *   Netlink socket file descriptor.
    1678                 :            :  * @param[in] name
    1679                 :            :  *   The family name.
    1680                 :            :  *
    1681                 :            :  * @return
    1682                 :            :  *   ID >= 0 on success and @p enable is updated, a negative errno value
    1683                 :            :  *   otherwise and rte_errno is set.
    1684                 :            :  */
    1685                 :            : static int
    1686                 :          0 : mlx5_nl_generic_family_id_get(int nlsk_fd, const char *name)
    1687                 :            : {
    1688                 :            :         struct nlmsghdr *nlh;
    1689                 :            :         struct genlmsghdr *genl;
    1690                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    1691                 :          0 :         int name_size = strlen(name) + 1;
    1692                 :            :         int ret;
    1693                 :          0 :         uint16_t id = -1;
    1694                 :            :         uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
    1695                 :            :                     NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
    1696                 :            :                     NLMSG_ALIGN(sizeof(struct nlattr)) +
    1697                 :            :                     NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE)];
    1698                 :            : 
    1699                 :            :         memset(buf, 0, sizeof(buf));
    1700                 :            :         nlh = (struct nlmsghdr *)buf;
    1701                 :            :         nlh->nlmsg_len = sizeof(struct nlmsghdr);
    1702                 :          0 :         nlh->nlmsg_type = GENL_ID_CTRL;
    1703                 :          0 :         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
    1704                 :            :         genl = (struct genlmsghdr *)nl_msg_tail(nlh);
    1705                 :          0 :         nlh->nlmsg_len += sizeof(struct genlmsghdr);
    1706                 :          0 :         genl->cmd = CTRL_CMD_GETFAMILY;
    1707                 :          0 :         genl->version = 1;
    1708                 :          0 :         nl_attr_put(nlh, CTRL_ATTR_FAMILY_NAME, name, name_size);
    1709                 :          0 :         ret = mlx5_nl_send(nlsk_fd, nlh, sn);
    1710         [ #  # ]:          0 :         if (ret >= 0)
    1711                 :          0 :                 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_family_id_cb, &id);
    1712         [ #  # ]:          0 :         if (ret < 0) {
    1713                 :          0 :                 DRV_LOG(DEBUG, "Failed to get Netlink %s family ID: %d.", name,
    1714                 :            :                         ret);
    1715                 :          0 :                 return ret;
    1716                 :            :         }
    1717                 :          0 :         DRV_LOG(DEBUG, "Netlink \"%s\" family ID is %u.", name, id);
    1718                 :          0 :         return (int)id;
    1719                 :            : }
    1720                 :            : 
    1721                 :            : /**
    1722                 :            :  * Get Devlink family ID.
    1723                 :            :  *
    1724                 :            :  * @param[in] nlsk_fd
    1725                 :            :  *   Netlink socket file descriptor.
    1726                 :            :  *
    1727                 :            :  * @return
    1728                 :            :  *   ID >= 0 on success and @p enable is updated, a negative errno value
    1729                 :            :  *   otherwise and rte_errno is set.
    1730                 :            :  */
    1731                 :            : 
    1732                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_devlink_family_id_get)
    1733                 :            : int
    1734                 :          0 : mlx5_nl_devlink_family_id_get(int nlsk_fd)
    1735                 :            : {
    1736                 :          0 :         return mlx5_nl_generic_family_id_get(nlsk_fd, DEVLINK_GENL_NAME);
    1737                 :            : }
    1738                 :            : 
    1739                 :            : /**
    1740                 :            :  * Parse Netlink message to retrieve the ROCE enable status.
    1741                 :            :  *
    1742                 :            :  * @param nh
    1743                 :            :  *   Pointer to Netlink Message Header.
    1744                 :            :  * @param arg
    1745                 :            :  *   PMD data register with this callback.
    1746                 :            :  *
    1747                 :            :  * @return
    1748                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1749                 :            :  */
    1750                 :            : static int
    1751                 :          0 : mlx5_nl_roce_cb(struct nlmsghdr *nh, void *arg)
    1752                 :            : {
    1753                 :            : 
    1754                 :            :         int ret = -EINVAL;
    1755                 :            :         int *enable = arg;
    1756                 :          0 :         struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
    1757                 :          0 :         struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
    1758                 :            :                                         NLMSG_ALIGN(sizeof(struct genlmsghdr)));
    1759                 :            : 
    1760   [ #  #  #  # ]:          0 :         while (nla->nla_len && nla < tail) {
    1761      [ #  #  # ]:          0 :                 switch (nla->nla_type) {
    1762                 :            :                 /* Expected nested attributes case. */
    1763                 :          0 :                 case DEVLINK_ATTR_PARAM:
    1764                 :            :                 case DEVLINK_ATTR_PARAM_VALUES_LIST:
    1765                 :            :                 case DEVLINK_ATTR_PARAM_VALUE:
    1766                 :            :                         ret = 0;
    1767                 :          0 :                         nla += 1;
    1768                 :          0 :                         break;
    1769                 :          0 :                 case DEVLINK_ATTR_PARAM_VALUE_DATA:
    1770                 :          0 :                         *enable = 1;
    1771                 :          0 :                         return 0;
    1772                 :          0 :                 default:
    1773                 :          0 :                         nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len));
    1774                 :            :                 }
    1775                 :            :         }
    1776                 :          0 :         *enable = 0;
    1777                 :          0 :         return ret;
    1778                 :            : }
    1779                 :            : 
    1780                 :            : /**
    1781                 :            :  * Get ROCE enable status through Netlink.
    1782                 :            :  *
    1783                 :            :  * @param[in] nlsk_fd
    1784                 :            :  *   Netlink socket file descriptor.
    1785                 :            :  * @param[in] family_id
    1786                 :            :  *   the Devlink family ID.
    1787                 :            :  * @param pci_addr
    1788                 :            :  *   The device PCI address.
    1789                 :            :  * @param[out] enable
    1790                 :            :  *   Where to store the enable status.
    1791                 :            :  *
    1792                 :            :  * @return
    1793                 :            :  *   0 on success and @p enable is updated, a negative errno value otherwise
    1794                 :            :  *   and rte_errno is set.
    1795                 :            :  */
    1796                 :            : int
    1797                 :          0 : mlx5_nl_enable_roce_get(int nlsk_fd, int family_id, const char *pci_addr,
    1798                 :            :                         int *enable)
    1799                 :            : {
    1800                 :            :         struct nlmsghdr *nlh;
    1801                 :            :         struct genlmsghdr *genl;
    1802                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    1803                 :            :         int ret;
    1804                 :          0 :         int cur_en = 0;
    1805                 :            :         uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
    1806                 :            :                     NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
    1807                 :            :                     NLMSG_ALIGN(sizeof(struct nlattr)) * 4 +
    1808                 :            :                     NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 4];
    1809                 :            : 
    1810                 :            :         memset(buf, 0, sizeof(buf));
    1811                 :            :         nlh = (struct nlmsghdr *)buf;
    1812                 :            :         nlh->nlmsg_len = sizeof(struct nlmsghdr);
    1813                 :          0 :         nlh->nlmsg_type = family_id;
    1814                 :          0 :         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
    1815                 :            :         genl = (struct genlmsghdr *)nl_msg_tail(nlh);
    1816                 :          0 :         nlh->nlmsg_len += sizeof(struct genlmsghdr);
    1817                 :          0 :         genl->cmd = DEVLINK_CMD_PARAM_GET;
    1818                 :          0 :         genl->version = DEVLINK_GENL_VERSION;
    1819                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
    1820                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
    1821                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
    1822                 :          0 :         ret = mlx5_nl_send(nlsk_fd, nlh, sn);
    1823         [ #  # ]:          0 :         if (ret >= 0)
    1824                 :          0 :                 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_roce_cb, &cur_en);
    1825         [ #  # ]:          0 :         if (ret < 0) {
    1826                 :          0 :                 DRV_LOG(DEBUG, "Failed to get ROCE enable on device %s: %d.",
    1827                 :            :                         pci_addr, ret);
    1828                 :          0 :                 return ret;
    1829                 :            :         }
    1830                 :          0 :         *enable = cur_en;
    1831         [ #  # ]:          0 :         DRV_LOG(DEBUG, "ROCE is %sabled for device \"%s\".",
    1832                 :            :                 cur_en ? "en" : "dis", pci_addr);
    1833                 :          0 :         return ret;
    1834                 :            : }
    1835                 :            : 
    1836                 :            : /**
    1837                 :            :  * Reload mlx5 device kernel driver through Netlink.
    1838                 :            :  *
    1839                 :            :  * @param[in] nlsk_fd
    1840                 :            :  *   Netlink socket file descriptor.
    1841                 :            :  * @param[in] family_id
    1842                 :            :  *   the Devlink family ID.
    1843                 :            :  * @param pci_addr
    1844                 :            :  *   The device PCI address.
    1845                 :            :  * @param[out] enable
    1846                 :            :  *   The enable status to set.
    1847                 :            :  *
    1848                 :            :  * @return
    1849                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1850                 :            :  */
    1851                 :            : static int
    1852                 :          0 : mlx5_nl_driver_reload(int nlsk_fd, int family_id, const char *pci_addr)
    1853                 :            : {
    1854                 :            :         struct nlmsghdr *nlh;
    1855                 :            :         struct genlmsghdr *genl;
    1856                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    1857                 :            :         int ret;
    1858                 :            :         uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
    1859                 :            :                     NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
    1860                 :            :                     NLMSG_ALIGN(sizeof(struct nlattr)) * 2 +
    1861                 :            :                     NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 2];
    1862                 :            : 
    1863                 :            :         memset(buf, 0, sizeof(buf));
    1864                 :            :         nlh = (struct nlmsghdr *)buf;
    1865                 :            :         nlh->nlmsg_len = sizeof(struct nlmsghdr);
    1866                 :          0 :         nlh->nlmsg_type = family_id;
    1867                 :          0 :         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
    1868                 :            :         genl = (struct genlmsghdr *)nl_msg_tail(nlh);
    1869                 :          0 :         nlh->nlmsg_len += sizeof(struct genlmsghdr);
    1870                 :          0 :         genl->cmd = DEVLINK_CMD_RELOAD;
    1871                 :          0 :         genl->version = DEVLINK_GENL_VERSION;
    1872                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
    1873                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
    1874                 :          0 :         ret = mlx5_nl_send(nlsk_fd, nlh, sn);
    1875         [ #  # ]:          0 :         if (ret >= 0)
    1876                 :          0 :                 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
    1877         [ #  # ]:          0 :         if (ret < 0) {
    1878                 :          0 :                 DRV_LOG(DEBUG, "Failed to reload %s device by Netlink - %d",
    1879                 :            :                         pci_addr, ret);
    1880                 :          0 :                 return ret;
    1881                 :            :         }
    1882                 :          0 :         DRV_LOG(DEBUG, "Device \"%s\" was reloaded by Netlink successfully.",
    1883                 :            :                 pci_addr);
    1884                 :          0 :         return 0;
    1885                 :            : }
    1886                 :            : 
    1887                 :            : /**
    1888                 :            :  * Set ROCE enable status through Netlink.
    1889                 :            :  *
    1890                 :            :  * @param[in] nlsk_fd
    1891                 :            :  *   Netlink socket file descriptor.
    1892                 :            :  * @param[in] family_id
    1893                 :            :  *   the Devlink family ID.
    1894                 :            :  * @param pci_addr
    1895                 :            :  *   The device PCI address.
    1896                 :            :  * @param[out] enable
    1897                 :            :  *   The enable status to set.
    1898                 :            :  *
    1899                 :            :  * @return
    1900                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1901                 :            :  */
    1902                 :            : int
    1903                 :          0 : mlx5_nl_enable_roce_set(int nlsk_fd, int family_id, const char *pci_addr,
    1904                 :            :                         int enable)
    1905                 :            : {
    1906                 :            :         struct nlmsghdr *nlh;
    1907                 :            :         struct genlmsghdr *genl;
    1908                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    1909                 :            :         int ret;
    1910                 :            :         uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
    1911                 :            :                     NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
    1912                 :            :                     NLMSG_ALIGN(sizeof(struct nlattr)) * 6 +
    1913                 :            :                     NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 6];
    1914                 :          0 :         uint8_t cmode = DEVLINK_PARAM_CMODE_DRIVERINIT;
    1915                 :          0 :         uint8_t ptype = NLA_FLAG;
    1916                 :            : ;
    1917                 :            : 
    1918                 :            :         memset(buf, 0, sizeof(buf));
    1919                 :            :         nlh = (struct nlmsghdr *)buf;
    1920                 :            :         nlh->nlmsg_len = sizeof(struct nlmsghdr);
    1921                 :          0 :         nlh->nlmsg_type = family_id;
    1922                 :          0 :         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
    1923                 :            :         genl = (struct genlmsghdr *)nl_msg_tail(nlh);
    1924                 :          0 :         nlh->nlmsg_len += sizeof(struct genlmsghdr);
    1925                 :          0 :         genl->cmd = DEVLINK_CMD_PARAM_SET;
    1926                 :          0 :         genl->version = DEVLINK_GENL_VERSION;
    1927                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
    1928                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
    1929                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME, "enable_roce", 12);
    1930                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_CMODE, &cmode, sizeof(cmode));
    1931                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_PARAM_TYPE, &ptype, sizeof(ptype));
    1932         [ #  # ]:          0 :         if (enable)
    1933                 :            :                 nl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, NULL, 0);
    1934                 :          0 :         ret = mlx5_nl_send(nlsk_fd, nlh, sn);
    1935         [ #  # ]:          0 :         if (ret >= 0)
    1936                 :          0 :                 ret = mlx5_nl_recv(nlsk_fd, sn, NULL, NULL);
    1937         [ #  # ]:          0 :         if (ret < 0) {
    1938         [ #  # ]:          0 :                 DRV_LOG(DEBUG, "Failed to %sable ROCE for device %s by Netlink:"
    1939                 :            :                         " %d.", enable ? "en" : "dis", pci_addr, ret);
    1940                 :          0 :                 return ret;
    1941                 :            :         }
    1942         [ #  # ]:          0 :         DRV_LOG(DEBUG, "Device %s ROCE was %sabled by Netlink successfully.",
    1943                 :            :                 pci_addr, enable ? "en" : "dis");
    1944                 :            :         /* Now, need to reload the driver. */
    1945                 :          0 :         return mlx5_nl_driver_reload(nlsk_fd, family_id, pci_addr);
    1946                 :            : }
    1947                 :            : 
    1948                 :            : /**
    1949                 :            :  * Try to parse a Netlink message as a link status update.
    1950                 :            :  *
    1951                 :            :  * @param hdr
    1952                 :            :  *  Netlink message header.
    1953                 :            :  * @param[out] ifindex
    1954                 :            :  *  Index of the updated interface.
    1955                 :            :  *
    1956                 :            :  * @return
    1957                 :            :  *  0 on success, negative on failure.
    1958                 :            :  */
    1959                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_parse_link_status_update)
    1960                 :            : int
    1961                 :          0 : mlx5_nl_parse_link_status_update(struct nlmsghdr *hdr, uint32_t *ifindex)
    1962                 :            : {
    1963                 :            :         struct ifinfomsg *info;
    1964                 :            : 
    1965         [ #  # ]:          0 :         switch (hdr->nlmsg_type) {
    1966                 :          0 :         case RTM_NEWLINK:
    1967                 :            :         case RTM_DELLINK:
    1968                 :            :         case RTM_GETLINK:
    1969                 :            :         case RTM_SETLINK:
    1970                 :            :                 info = NLMSG_DATA(hdr);
    1971                 :          0 :                 *ifindex = info->ifi_index;
    1972                 :          0 :                 return 0;
    1973                 :            :         }
    1974                 :            :         return -1;
    1975                 :            : }
    1976                 :            : 
    1977                 :            : /**
    1978                 :            :  * Read pending events from a Netlink socket.
    1979                 :            :  *
    1980                 :            :  * @param nlsk_fd
    1981                 :            :  *  Netlink socket.
    1982                 :            :  * @param cb
    1983                 :            :  *  Callback invoked for each of the events.
    1984                 :            :  * @param cb_arg
    1985                 :            :  *  User data for the callback.
    1986                 :            :  *
    1987                 :            :  * @return
    1988                 :            :  *  0 on success, including the case when there are no events.
    1989                 :            :  *  Negative on failure and rte_errno is set.
    1990                 :            :  */
    1991                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_read_events)
    1992                 :            : int
    1993                 :          0 : mlx5_nl_read_events(int nlsk_fd, mlx5_nl_event_cb *cb, void *cb_arg)
    1994                 :            : {
    1995                 :            :         char buf[8192];
    1996                 :            :         struct sockaddr_nl addr;
    1997                 :          0 :         struct iovec iov = {
    1998                 :            :                 .iov_base = buf,
    1999                 :            :                 .iov_len = sizeof(buf),
    2000                 :            :         };
    2001                 :          0 :         struct msghdr msg = {
    2002                 :            :                 .msg_name = &addr,
    2003                 :            :                 .msg_namelen = sizeof(addr),
    2004                 :            :                 .msg_iov = &iov,
    2005                 :            :                 .msg_iovlen = 1,
    2006                 :            :         };
    2007                 :            :         struct nlmsghdr *hdr;
    2008                 :            :         ssize_t size;
    2009                 :            : 
    2010                 :            :         while (1) {
    2011                 :          0 :                 size = recvmsg(nlsk_fd, &msg, MSG_DONTWAIT);
    2012         [ #  # ]:          0 :                 if (size < 0) {
    2013         [ #  # ]:          0 :                         if (errno == EAGAIN)
    2014                 :            :                                 return 0;
    2015         [ #  # ]:          0 :                         if (errno == EINTR)
    2016                 :          0 :                                 continue;
    2017                 :          0 :                         DRV_LOG(DEBUG, "Failed to receive netlink message: %s",
    2018                 :            :                                 strerror(errno));
    2019                 :          0 :                         rte_errno = errno;
    2020                 :          0 :                         return -rte_errno;
    2021                 :            :                 }
    2022                 :            :                 hdr = (struct nlmsghdr *)buf;
    2023         [ #  # ]:          0 :                 while (size >= (ssize_t)sizeof(*hdr)) {
    2024                 :          0 :                         ssize_t msg_len = hdr->nlmsg_len;
    2025                 :          0 :                         ssize_t data_len = msg_len - sizeof(*hdr);
    2026                 :            :                         ssize_t aligned_len;
    2027                 :            : 
    2028         [ #  # ]:          0 :                         if (data_len < 0) {
    2029                 :          0 :                                 DRV_LOG(DEBUG, "Netlink message too short");
    2030                 :          0 :                                 rte_errno = EINVAL;
    2031                 :          0 :                                 return -rte_errno;
    2032                 :            :                         }
    2033                 :          0 :                         aligned_len = NLMSG_ALIGN(msg_len);
    2034         [ #  # ]:          0 :                         if (aligned_len > size) {
    2035                 :          0 :                                 DRV_LOG(DEBUG, "Netlink message too long");
    2036                 :          0 :                                 rte_errno = EINVAL;
    2037                 :          0 :                                 return -rte_errno;
    2038                 :            :                         }
    2039                 :          0 :                         cb(hdr, cb_arg);
    2040                 :          0 :                         hdr = RTE_PTR_ADD(hdr, aligned_len);
    2041                 :          0 :                         size -= aligned_len;
    2042                 :            :                 }
    2043                 :            :         }
    2044                 :            :         return 0;
    2045                 :            : }
    2046                 :            : 
    2047                 :            : static int
    2048                 :          0 : mlx5_nl_esw_multiport_cb(struct nlmsghdr *nh, void *arg)
    2049                 :            : {
    2050                 :            : 
    2051                 :            :         int ret = -EINVAL;
    2052                 :            :         int *enable = arg;
    2053                 :          0 :         struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
    2054                 :          0 :         struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
    2055                 :            :                                         NLMSG_ALIGN(sizeof(struct genlmsghdr)));
    2056                 :            : 
    2057   [ #  #  #  # ]:          0 :         while (nla->nla_len && nla < tail) {
    2058      [ #  #  # ]:          0 :                 switch (nla->nla_type) {
    2059                 :            :                 /* Expected nested attributes case. */
    2060                 :          0 :                 case DEVLINK_ATTR_PARAM:
    2061                 :            :                 case DEVLINK_ATTR_PARAM_VALUES_LIST:
    2062                 :            :                 case DEVLINK_ATTR_PARAM_VALUE:
    2063                 :            :                         ret = 0;
    2064                 :          0 :                         nla += 1;
    2065                 :          0 :                         break;
    2066                 :          0 :                 case DEVLINK_ATTR_PARAM_VALUE_DATA:
    2067                 :          0 :                         *enable = 1;
    2068                 :          0 :                         return 0;
    2069                 :          0 :                 default:
    2070                 :          0 :                         nla = RTE_PTR_ADD(nla, NLMSG_ALIGN(nla->nla_len));
    2071                 :            :                 }
    2072                 :            :         }
    2073                 :          0 :         *enable = 0;
    2074                 :          0 :         return ret;
    2075                 :            : }
    2076                 :            : 
    2077                 :            : #define NL_ESW_MULTIPORT_PARAM "esw_multiport"
    2078                 :            : 
    2079                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_devlink_esw_multiport_get)
    2080                 :            : int
    2081                 :          0 : mlx5_nl_devlink_esw_multiport_get(int nlsk_fd, int family_id, const char *pci_addr, int *enable)
    2082                 :            : {
    2083                 :            :         struct nlmsghdr *nlh;
    2084                 :            :         struct genlmsghdr *genl;
    2085                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    2086                 :            :         int ret;
    2087                 :            :         uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
    2088                 :            :                     NLMSG_ALIGN(sizeof(struct genlmsghdr)) +
    2089                 :            :                     NLMSG_ALIGN(sizeof(struct nlattr)) * 4 +
    2090                 :            :                     NLMSG_ALIGN(MLX5_NL_MAX_ATTR_SIZE) * 4];
    2091                 :            : 
    2092                 :            :         memset(buf, 0, sizeof(buf));
    2093                 :            :         nlh = (struct nlmsghdr *)buf;
    2094                 :            :         nlh->nlmsg_len = sizeof(struct nlmsghdr);
    2095                 :          0 :         nlh->nlmsg_type = family_id;
    2096                 :          0 :         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
    2097                 :            :         genl = (struct genlmsghdr *)nl_msg_tail(nlh);
    2098                 :          0 :         nlh->nlmsg_len += sizeof(struct genlmsghdr);
    2099                 :          0 :         genl->cmd = DEVLINK_CMD_PARAM_GET;
    2100                 :          0 :         genl->version = DEVLINK_GENL_VERSION;
    2101                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_BUS_NAME, "pci", 4);
    2102                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_DEV_NAME, pci_addr, strlen(pci_addr) + 1);
    2103                 :          0 :         nl_attr_put(nlh, DEVLINK_ATTR_PARAM_NAME,
    2104                 :            :                     NL_ESW_MULTIPORT_PARAM, sizeof(NL_ESW_MULTIPORT_PARAM));
    2105                 :          0 :         ret = mlx5_nl_send(nlsk_fd, nlh, sn);
    2106         [ #  # ]:          0 :         if (ret >= 0)
    2107                 :          0 :                 ret = mlx5_nl_recv(nlsk_fd, sn, mlx5_nl_esw_multiport_cb, enable);
    2108         [ #  # ]:          0 :         if (ret < 0) {
    2109                 :          0 :                 DRV_LOG(DEBUG, "Failed to get Multiport E-Switch enable on device %s: %d.",
    2110                 :            :                         pci_addr, ret);
    2111                 :          0 :                 return ret;
    2112                 :            :         }
    2113         [ #  # ]:          0 :         DRV_LOG(DEBUG, "Multiport E-Switch is %sabled for device \"%s\".",
    2114                 :            :                 *enable ? "en" : "dis", pci_addr);
    2115                 :          0 :         return ret;
    2116                 :            : }
    2117                 :            : 
    2118                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_rdma_monitor_init)
    2119                 :            : int
    2120                 :          0 : mlx5_nl_rdma_monitor_init(void)
    2121                 :            : {
    2122                 :          0 :         return mlx5_nl_init(NETLINK_RDMA, RDMA_NL_GROUP_NOTIFICATION);
    2123                 :            : }
    2124                 :            : 
    2125                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_rdma_monitor_info_get)
    2126                 :            : void
    2127                 :          0 : mlx5_nl_rdma_monitor_info_get(struct nlmsghdr *hdr, struct mlx5_nl_port_info *data)
    2128                 :            : {
    2129                 :            :         size_t off = NLMSG_HDRLEN;
    2130                 :            :         uint8_t event_type = 0;
    2131                 :            : 
    2132         [ #  # ]:          0 :         if (hdr->nlmsg_type != RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_MONITOR))
    2133                 :          0 :                 goto error;
    2134                 :            : 
    2135         [ #  # ]:          0 :         while (off < hdr->nlmsg_len) {
    2136                 :          0 :                 struct nlattr *na = (void *)((uintptr_t)hdr + off);
    2137                 :          0 :                 void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
    2138                 :            : 
    2139         [ #  # ]:          0 :                 if (na->nla_len > hdr->nlmsg_len - off)
    2140                 :          0 :                         goto error;
    2141   [ #  #  #  #  :          0 :                 switch (na->nla_type) {
                      # ]
    2142                 :          0 :                 case RDMA_NLDEV_ATTR_EVENT_TYPE:
    2143                 :          0 :                         event_type = *(uint8_t *)payload;
    2144         [ #  # ]:          0 :                         if (event_type == RDMA_NETDEV_ATTACH_EVENT) {
    2145                 :          0 :                                 data->flags |= MLX5_NL_CMD_GET_EVENT_TYPE;
    2146                 :          0 :                                 data->event_type = MLX5_NL_RDMA_NETDEV_ATTACH_EVENT;
    2147         [ #  # ]:          0 :                         } else if (event_type == RDMA_NETDEV_DETACH_EVENT) {
    2148                 :          0 :                                 data->flags |= MLX5_NL_CMD_GET_EVENT_TYPE;
    2149                 :          0 :                                 data->event_type = MLX5_NL_RDMA_NETDEV_DETACH_EVENT;
    2150                 :            :                         }
    2151                 :            :                         break;
    2152                 :          0 :                 case RDMA_NLDEV_ATTR_DEV_INDEX:
    2153                 :          0 :                         data->ibindex = *(uint32_t *)payload;
    2154                 :          0 :                         data->flags |= MLX5_NL_CMD_GET_IB_INDEX;
    2155                 :          0 :                         break;
    2156                 :          0 :                 case RDMA_NLDEV_ATTR_PORT_INDEX:
    2157                 :          0 :                         data->portnum = *(uint32_t *)payload;
    2158                 :          0 :                         data->flags |= MLX5_NL_CMD_GET_PORT_INDEX;
    2159                 :          0 :                         break;
    2160                 :          0 :                 case RDMA_NLDEV_ATTR_NDEV_INDEX:
    2161                 :          0 :                         data->ifindex = *(uint32_t *)payload;
    2162                 :          0 :                         data->flags |= MLX5_NL_CMD_GET_NET_INDEX;
    2163                 :          0 :                         break;
    2164                 :          0 :                 default:
    2165                 :          0 :                         DRV_LOG(DEBUG, "Unknown attribute[%d] found", na->nla_type);
    2166                 :          0 :                         break;
    2167                 :            :                 }
    2168                 :          0 :                 off += NLA_ALIGN(na->nla_len);
    2169                 :            :         }
    2170                 :            : 
    2171                 :            :         return;
    2172                 :            : 
    2173                 :          0 : error:
    2174                 :          0 :         rte_errno = EINVAL;
    2175                 :            : }
    2176                 :            : 
    2177                 :            : static int
    2178                 :          0 : mlx5_nl_rdma_monitor_cap_get_cb(struct nlmsghdr *hdr, void *arg)
    2179                 :            : {
    2180                 :            :         size_t off = NLMSG_HDRLEN;
    2181                 :            :         uint8_t *cap = arg;
    2182                 :            : 
    2183         [ #  # ]:          0 :         if (hdr->nlmsg_type != RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_SYS_GET))
    2184                 :          0 :                 goto error;
    2185                 :            : 
    2186                 :          0 :         *cap = 0;
    2187         [ #  # ]:          0 :         while (off < hdr->nlmsg_len) {
    2188                 :          0 :                 struct nlattr *na = (void *)((uintptr_t)hdr + off);
    2189                 :          0 :                 void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
    2190                 :            : 
    2191         [ #  # ]:          0 :                 if (na->nla_len > hdr->nlmsg_len - off)
    2192                 :          0 :                         goto error;
    2193         [ #  # ]:          0 :                 switch (na->nla_type) {
    2194                 :          0 :                 case RDMA_NLDEV_SYS_ATTR_MONITOR_MODE:
    2195                 :          0 :                         *cap = *(uint8_t *)payload;
    2196                 :          0 :                         return 0;
    2197                 :            :                 default:
    2198                 :            :                         break;
    2199                 :            :                 }
    2200                 :          0 :                 off += NLA_ALIGN(na->nla_len);
    2201                 :            :         }
    2202                 :            : 
    2203                 :            :         return 0;
    2204                 :            : 
    2205                 :            : error:
    2206                 :            :         return -EINVAL;
    2207                 :            : }
    2208                 :            : 
    2209                 :            : /**
    2210                 :            :  * Get RDMA monitor support in driver.
    2211                 :            :  *
    2212                 :            :  *
    2213                 :            :  * @param nl
    2214                 :            :  *   Netlink socket of the RDMA kind (NETLINK_RDMA).
    2215                 :            :  * @param[out] cap
    2216                 :            :  *   Pointer to port info.
    2217                 :            :  * @return
    2218                 :            :  *   0 on success, negative on error and rte_errno is set.
    2219                 :            :  */
    2220                 :            : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_nl_rdma_monitor_cap_get)
    2221                 :            : int
    2222                 :          0 : mlx5_nl_rdma_monitor_cap_get(int nl, uint8_t *cap)
    2223                 :            : {
    2224                 :            :         union {
    2225                 :            :                 struct nlmsghdr nh;
    2226                 :            :                 uint8_t buf[NLMSG_HDRLEN];
    2227                 :          0 :         } req = {
    2228                 :            :                 .nh = {
    2229                 :            :                         .nlmsg_len = NLMSG_LENGTH(0),
    2230                 :            :                         .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
    2231                 :            :                                                        RDMA_NLDEV_CMD_SYS_GET),
    2232                 :            :                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
    2233                 :            :                 },
    2234                 :            :         };
    2235                 :          0 :         uint32_t sn = MLX5_NL_SN_GENERATE;
    2236                 :            :         int ret;
    2237                 :            : 
    2238                 :          0 :         ret = mlx5_nl_send(nl, &req.nh, sn);
    2239         [ #  # ]:          0 :         if (ret < 0) {
    2240                 :          0 :                 rte_errno = -ret;
    2241                 :          0 :                 return ret;
    2242                 :            :         }
    2243                 :          0 :         ret = mlx5_nl_recv(nl, sn, mlx5_nl_rdma_monitor_cap_get_cb, cap);
    2244         [ #  # ]:          0 :         if (ret < 0) {
    2245                 :          0 :                 rte_errno = -ret;
    2246                 :          0 :                 return ret;
    2247                 :            :         }
    2248                 :            :         return 0;
    2249                 :            : }

Generated by: LCOV version 1.14