LCOV - code coverage report
Current view: top level - drivers/net/mlx5/linux - mlx5_socket.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 101 0.0 %
Date: 2025-03-01 20:23:48 Functions: 0 3 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 54 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright 2019 Mellanox Technologies, Ltd
       3                 :            :  */
       4                 :            : 
       5                 :            : #ifndef _GNU_SOURCE
       6                 :            : #define _GNU_SOURCE
       7                 :            : #endif
       8                 :            : 
       9                 :            : #include <sys/types.h>
      10                 :            : #include <sys/socket.h>
      11                 :            : #include <sys/un.h>
      12                 :            : #include <fcntl.h>
      13                 :            : #include <stdio.h>
      14                 :            : #include <unistd.h>
      15                 :            : #include <sys/stat.h>
      16                 :            : 
      17                 :            : #include "rte_eal.h"
      18                 :            : #include "mlx5_utils.h"
      19                 :            : #include "mlx5.h"
      20                 :            : 
      21                 :            : /* PMD socket service for tools. */
      22                 :            : 
      23                 :            : #define MLX5_SOCKET_PATH "/var/tmp/dpdk_net_mlx5_%d"
      24                 :            : #define MLX5_ALL_PORT_IDS 0xffff
      25                 :            : 
      26                 :            : int server_socket = -1; /* Unix socket for primary process. */
      27                 :            : struct rte_intr_handle *server_intr_handle; /* Interrupt handler. */
      28                 :            : 
      29                 :            : /**
      30                 :            :  * Handle server pmd socket interrupts.
      31                 :            :  */
      32                 :            : static void
      33                 :          0 : mlx5_pmd_socket_handle(void *cb __rte_unused)
      34                 :            : {
      35                 :            :         int conn_sock;
      36                 :            :         int ret;
      37                 :            :         struct cmsghdr *cmsg = NULL;
      38                 :            :         uint32_t data[MLX5_SENDMSG_MAX / sizeof(uint32_t)];
      39                 :            :         struct rte_flow *flow_ptr = NULL;
      40                 :          0 :         uint8_t  buf[CMSG_SPACE(sizeof(int))] = { 0 };
      41                 :          0 :         struct iovec io = {
      42                 :            :                 .iov_base = data,
      43                 :            :                 .iov_len = sizeof(data),
      44                 :            :         };
      45                 :          0 :         struct msghdr msg = {
      46                 :            :                 .msg_iov = &io,
      47                 :            :                 .msg_iovlen = 1,
      48                 :            :                 .msg_control = buf,
      49                 :            :                 .msg_controllen = sizeof(buf),
      50                 :            :         };
      51                 :            : 
      52                 :            :         uint32_t port_id;
      53                 :            :         int fd;
      54                 :            :         FILE *file = NULL;
      55                 :            :         struct rte_eth_dev *dev;
      56                 :            :         struct rte_flow_error err;
      57                 :            :         struct mlx5_flow_dump_req  *dump_req;
      58                 :            :         struct mlx5_flow_dump_ack  *dump_ack;
      59                 :            : 
      60                 :            :         memset(data, 0, sizeof(data));
      61                 :            :         /* Accept the connection from the client. */
      62                 :          0 :         conn_sock = accept(server_socket, NULL, NULL);
      63         [ #  # ]:          0 :         if (conn_sock < 0) {
      64                 :          0 :                 DRV_LOG(WARNING, "connection failed: %s", strerror(errno));
      65                 :          0 :                 return;
      66                 :            :         }
      67                 :          0 :         ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
      68         [ #  # ]:          0 :         if (ret != sizeof(struct mlx5_flow_dump_req)) {
      69                 :          0 :                 DRV_LOG(WARNING, "wrong message received: %s",
      70                 :            :                         strerror(errno));
      71                 :          0 :                 goto error;
      72                 :            :         }
      73                 :            : 
      74                 :            :         /* Receive file descriptor. */
      75         [ #  # ]:          0 :         cmsg = CMSG_FIRSTHDR(&msg);
      76   [ #  #  #  # ]:          0 :         if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS ||
      77         [ #  # ]:          0 :             cmsg->cmsg_len < sizeof(int)) {
      78                 :          0 :                 DRV_LOG(WARNING, "invalid file descriptor message");
      79                 :          0 :                 goto error;
      80                 :            :         }
      81                 :            :         memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
      82                 :          0 :         file = fdopen(fd, "w");
      83         [ #  # ]:          0 :         if (!file) {
      84                 :          0 :                 DRV_LOG(WARNING, "Failed to open file");
      85                 :          0 :                 goto error;
      86                 :            :         }
      87                 :            :         /* Receive port number. */
      88   [ #  #  #  # ]:          0 :         if (msg.msg_iovlen != 1 || msg.msg_iov->iov_len < sizeof(uint16_t)) {
      89                 :          0 :                 DRV_LOG(WARNING, "wrong port number message");
      90                 :          0 :                 goto error;
      91                 :            :         }
      92                 :            : 
      93                 :          0 :         dump_req = (struct mlx5_flow_dump_req *)msg.msg_iov->iov_base;
      94         [ #  # ]:          0 :         if (dump_req) {
      95                 :          0 :                 port_id = dump_req->port_id;
      96                 :          0 :                 flow_ptr = (struct rte_flow *)((uintptr_t)dump_req->flow_id);
      97                 :            :         } else {
      98                 :          0 :                 DRV_LOG(WARNING, "Invalid message");
      99                 :          0 :                 goto error;
     100                 :            :         }
     101                 :            : 
     102         [ #  # ]:          0 :         if (port_id == MLX5_ALL_PORT_IDS) {
     103                 :            :                 /* Dump all port ids */
     104         [ #  # ]:          0 :                 if (flow_ptr) {
     105                 :          0 :                         DRV_LOG(WARNING, "Flow ptr unsupported with given port id");
     106                 :          0 :                         goto error;
     107                 :            :                 }
     108                 :            : 
     109         [ #  # ]:          0 :                 MLX5_ETH_FOREACH_DEV(port_id, NULL) {
     110                 :          0 :                         dev = &rte_eth_devices[port_id];
     111                 :          0 :                         ret = mlx5_flow_dev_dump(dev, NULL, file, &err);
     112         [ #  # ]:          0 :                         if (ret)
     113                 :            :                                 break;
     114                 :            :                 }
     115                 :            :         } else {
     116                 :            :                 /* Dump single port id */
     117         [ #  # ]:          0 :                 if (!rte_eth_dev_is_valid_port(port_id)) {
     118                 :          0 :                         DRV_LOG(WARNING, "Invalid port %u", port_id);
     119                 :          0 :                         goto error;
     120                 :            :                 }
     121                 :            : 
     122                 :            :                 /* Dump flow */
     123                 :          0 :                 dev = &rte_eth_devices[port_id];
     124                 :          0 :                 ret = mlx5_flow_dev_dump(dev, flow_ptr, file, &err);
     125                 :            :         }
     126                 :            : 
     127                 :            :         /* Set-up the ancillary data and reply. */
     128                 :          0 :         msg.msg_controllen = 0;
     129                 :          0 :         msg.msg_control = NULL;
     130                 :          0 :         msg.msg_iovlen = 1;
     131                 :          0 :         msg.msg_iov = &io;
     132                 :            :         dump_ack = (struct mlx5_flow_dump_ack *)data;
     133                 :          0 :         dump_ack->rc = -ret;
     134                 :          0 :         io.iov_len = sizeof(struct mlx5_flow_dump_ack);
     135                 :          0 :         io.iov_base = dump_ack;
     136                 :            :         do {
     137                 :          0 :                 ret = sendmsg(conn_sock, &msg, 0);
     138   [ #  #  #  # ]:          0 :         } while (ret < 0 && errno == EINTR);
     139         [ #  # ]:          0 :         if (ret < 0)
     140                 :          0 :                 DRV_LOG(WARNING, "failed to send response %s",
     141                 :            :                         strerror(errno));
     142                 :          0 : error:
     143                 :            :         if (conn_sock >= 0)
     144                 :          0 :                 close(conn_sock);
     145         [ #  # ]:          0 :         if (file)
     146                 :          0 :                 fclose(file);
     147                 :            : }
     148                 :            : 
     149                 :            : /**
     150                 :            :  * Initialise the socket to communicate with external tools.
     151                 :            :  *
     152                 :            :  * @return
     153                 :            :  *   0 on success, a negative value otherwise.
     154                 :            :  */
     155                 :            : int
     156                 :          0 : mlx5_pmd_socket_init(void)
     157                 :            : {
     158                 :          0 :         struct sockaddr_un sun = {
     159                 :            :                 .sun_family = AF_UNIX,
     160                 :            :         };
     161                 :            :         int ret;
     162                 :            :         int flags;
     163                 :            : 
     164                 :            :         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
     165         [ #  # ]:          0 :         if (server_socket != -1)
     166                 :            :                 return 0;
     167                 :          0 :         ret = socket(AF_UNIX, SOCK_STREAM, 0);
     168         [ #  # ]:          0 :         if (ret < 0) {
     169                 :          0 :                 DRV_LOG(WARNING, "Failed to open mlx5 socket: %s",
     170                 :            :                         strerror(errno));
     171                 :          0 :                 goto error;
     172                 :            :         }
     173                 :          0 :         server_socket = ret;
     174                 :          0 :         flags = fcntl(server_socket, F_GETFL, 0);
     175         [ #  # ]:          0 :         if (flags == -1)
     176                 :          0 :                 goto close;
     177                 :          0 :         ret = fcntl(server_socket, F_SETFL, flags | O_NONBLOCK);
     178         [ #  # ]:          0 :         if (ret < 0)
     179                 :          0 :                 goto close;
     180                 :          0 :         snprintf(sun.sun_path, sizeof(sun.sun_path), MLX5_SOCKET_PATH,
     181                 :            :                  getpid());
     182                 :          0 :         remove(sun.sun_path);
     183                 :          0 :         ret = bind(server_socket, (const struct sockaddr *)&sun, sizeof(sun));
     184         [ #  # ]:          0 :         if (ret < 0) {
     185                 :          0 :                 DRV_LOG(WARNING,
     186                 :            :                         "cannot bind mlx5 socket: %s", strerror(errno));
     187                 :          0 :                 goto remove;
     188                 :            :         }
     189                 :          0 :         ret = listen(server_socket, 0);
     190         [ #  # ]:          0 :         if (ret < 0) {
     191                 :          0 :                 DRV_LOG(WARNING, "cannot listen on mlx5 socket: %s",
     192                 :            :                         strerror(errno));
     193                 :          0 :                 goto remove;
     194                 :            :         }
     195                 :          0 :         server_intr_handle = mlx5_os_interrupt_handler_create
     196                 :            :                 (RTE_INTR_INSTANCE_F_PRIVATE, false,
     197                 :            :                  server_socket, mlx5_pmd_socket_handle, NULL);
     198         [ #  # ]:          0 :         if (server_intr_handle == NULL) {
     199                 :          0 :                 DRV_LOG(WARNING, "cannot register interrupt handler for mlx5 socket: %s",
     200                 :            :                         strerror(errno));
     201                 :          0 :                 goto remove;
     202                 :            :         }
     203                 :            :         return 0;
     204                 :          0 : remove:
     205                 :          0 :         remove(sun.sun_path);
     206                 :          0 : close:
     207                 :          0 :         claim_zero(close(server_socket));
     208                 :          0 :         server_socket = -1;
     209                 :          0 : error:
     210                 :          0 :         DRV_LOG(ERR, "Cannot initialize socket: %s", strerror(errno));
     211                 :          0 :         return -errno;
     212                 :            : }
     213                 :            : 
     214                 :            : /**
     215                 :            :  * Un-Initialize the pmd socket
     216                 :            :  */
     217                 :            : void
     218                 :          0 : mlx5_pmd_socket_uninit(void)
     219                 :            : {
     220         [ #  # ]:          0 :         if (server_socket == -1)
     221                 :            :                 return;
     222                 :          0 :         mlx5_os_interrupt_handler_destroy(server_intr_handle,
     223                 :            :                                           mlx5_pmd_socket_handle, NULL);
     224                 :          0 :         claim_zero(close(server_socket));
     225                 :          0 :         server_socket = -1;
     226                 :          0 :         MKSTR(path, MLX5_SOCKET_PATH, getpid());
     227                 :          0 :         claim_zero(remove(path));
     228                 :            : }

Generated by: LCOV version 1.14