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

Generated by: LCOV version 1.14