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 25 int server_socket; /* Unix socket for primary process. */ 26 struct rte_intr_handle server_intr_handle; /* Interrupt handler. */ 27 28 /** 29 * Handle server pmd socket interrupts. 30 */ 31 static void 32 mlx5_pmd_socket_handle(void *cb __rte_unused) 33 { 34 int conn_sock; 35 int ret; 36 struct cmsghdr *cmsg = NULL; 37 uint32_t data[MLX5_SENDMSG_MAX / sizeof(uint32_t)]; 38 uint64_t flow_ptr = 0; 39 uint8_t buf[CMSG_SPACE(sizeof(int))] = { 0 }; 40 struct iovec io = { 41 .iov_base = data, 42 .iov_len = sizeof(data), 43 }; 44 struct msghdr msg = { 45 .msg_iov = &io, 46 .msg_iovlen = 1, 47 .msg_control = buf, 48 .msg_controllen = sizeof(buf), 49 }; 50 51 uint32_t port_id; 52 int fd; 53 FILE *file = NULL; 54 struct rte_eth_dev *dev; 55 struct rte_flow_error err; 56 struct mlx5_flow_dump_req *dump_req; 57 struct mlx5_flow_dump_ack *dump_ack; 58 59 memset(data, 0, sizeof(data)); 60 /* Accept the connection from the client. */ 61 conn_sock = accept(server_socket, NULL, NULL); 62 if (conn_sock < 0) { 63 DRV_LOG(WARNING, "connection failed: %s", strerror(errno)); 64 return; 65 } 66 ret = recvmsg(conn_sock, &msg, MSG_WAITALL); 67 if (ret != sizeof(struct mlx5_flow_dump_req)) { 68 DRV_LOG(WARNING, "wrong message received: %s", 69 strerror(errno)); 70 goto error; 71 } 72 73 /* Receive file descriptor. */ 74 cmsg = CMSG_FIRSTHDR(&msg); 75 if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS || 76 cmsg->cmsg_len < sizeof(int)) { 77 DRV_LOG(WARNING, "invalid file descriptor message"); 78 goto error; 79 } 80 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd)); 81 file = fdopen(fd, "w"); 82 if (!file) { 83 DRV_LOG(WARNING, "Failed to open file"); 84 goto error; 85 } 86 /* Receive port number. */ 87 if (msg.msg_iovlen != 1 || msg.msg_iov->iov_len < sizeof(uint16_t)) { 88 DRV_LOG(WARNING, "wrong port number message"); 89 goto error; 90 } 91 92 dump_req = (struct mlx5_flow_dump_req *)msg.msg_iov->iov_base; 93 if (dump_req) { 94 port_id = dump_req->port_id; 95 flow_ptr = dump_req->flow_id; 96 } else { 97 DRV_LOG(WARNING, "Invalid message"); 98 goto error; 99 } 100 101 if (!rte_eth_dev_is_valid_port(port_id)) { 102 DRV_LOG(WARNING, "Invalid port %u", port_id); 103 goto error; 104 } 105 106 /* Dump flow. */ 107 dev = &rte_eth_devices[port_id]; 108 if (flow_ptr == 0) 109 ret = mlx5_flow_dev_dump(dev, NULL, file, NULL); 110 else 111 ret = mlx5_flow_dev_dump(dev, 112 (struct rte_flow *)((uintptr_t)flow_ptr), file, &err); 113 114 /* Set-up the ancillary data and reply. */ 115 msg.msg_controllen = 0; 116 msg.msg_control = NULL; 117 msg.msg_iovlen = 1; 118 msg.msg_iov = &io; 119 dump_ack = (struct mlx5_flow_dump_ack *)data; 120 dump_ack->rc = -ret; 121 io.iov_len = sizeof(struct mlx5_flow_dump_ack); 122 io.iov_base = dump_ack; 123 do { 124 ret = sendmsg(conn_sock, &msg, 0); 125 } while (ret < 0 && errno == EINTR); 126 if (ret < 0) 127 DRV_LOG(WARNING, "failed to send response %s", 128 strerror(errno)); 129 error: 130 if (conn_sock >= 0) 131 close(conn_sock); 132 if (file) 133 fclose(file); 134 } 135 136 /** 137 * Install interrupt handler. 138 * 139 * @param dev 140 * Pointer to Ethernet device. 141 * @return 142 * 0 on success, a negative errno value otherwise. 143 */ 144 static int 145 mlx5_pmd_interrupt_handler_install(void) 146 { 147 MLX5_ASSERT(server_socket); 148 server_intr_handle.fd = server_socket; 149 server_intr_handle.type = RTE_INTR_HANDLE_EXT; 150 return rte_intr_callback_register(&server_intr_handle, 151 mlx5_pmd_socket_handle, NULL); 152 } 153 154 /** 155 * Uninstall interrupt handler. 156 */ 157 static void 158 mlx5_pmd_interrupt_handler_uninstall(void) 159 { 160 if (server_socket) { 161 mlx5_intr_callback_unregister(&server_intr_handle, 162 mlx5_pmd_socket_handle, 163 NULL); 164 } 165 server_intr_handle.fd = 0; 166 server_intr_handle.type = RTE_INTR_HANDLE_UNKNOWN; 167 } 168 169 /** 170 * Initialise the socket to communicate with the secondary process 171 * 172 * @param[in] dev 173 * Pointer to Ethernet device. 174 * 175 * @return 176 * 0 on success, a negative value otherwise. 177 */ 178 int 179 mlx5_pmd_socket_init(void) 180 { 181 struct sockaddr_un sun = { 182 .sun_family = AF_UNIX, 183 }; 184 int ret; 185 int flags; 186 187 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY); 188 if (server_socket) 189 return 0; 190 /* 191 * Initialize the socket to communicate with the secondary 192 * process. 193 */ 194 ret = socket(AF_UNIX, SOCK_STREAM, 0); 195 if (ret < 0) { 196 DRV_LOG(WARNING, "Failed to open mlx5 socket: %s", 197 strerror(errno)); 198 goto error; 199 } 200 server_socket = ret; 201 flags = fcntl(server_socket, F_GETFL, 0); 202 if (flags == -1) 203 goto error; 204 ret = fcntl(server_socket, F_SETFL, flags | O_NONBLOCK); 205 if (ret < 0) 206 goto error; 207 snprintf(sun.sun_path, sizeof(sun.sun_path), MLX5_SOCKET_PATH, 208 getpid()); 209 remove(sun.sun_path); 210 ret = bind(server_socket, (const struct sockaddr *)&sun, sizeof(sun)); 211 if (ret < 0) { 212 DRV_LOG(WARNING, 213 "cannot bind mlx5 socket: %s", strerror(errno)); 214 goto close; 215 } 216 ret = listen(server_socket, 0); 217 if (ret < 0) { 218 DRV_LOG(WARNING, "cannot listen on mlx5 socket: %s", 219 strerror(errno)); 220 goto close; 221 } 222 if (mlx5_pmd_interrupt_handler_install()) { 223 DRV_LOG(WARNING, "cannot register interrupt handler for mlx5 socket: %s", 224 strerror(errno)); 225 goto close; 226 } 227 return 0; 228 close: 229 remove(sun.sun_path); 230 error: 231 claim_zero(close(server_socket)); 232 server_socket = 0; 233 DRV_LOG(ERR, "Cannot initialize socket: %s", strerror(errno)); 234 return -errno; 235 } 236 237 /** 238 * Un-Initialize the pmd socket 239 */ 240 RTE_FINI(mlx5_pmd_socket_uninit) 241 { 242 if (!server_socket) 243 return; 244 mlx5_pmd_interrupt_handler_uninstall(); 245 claim_zero(close(server_socket)); 246 server_socket = 0; 247 MKSTR(path, MLX5_SOCKET_PATH, getpid()); 248 claim_zero(remove(path)); 249 } 250