1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
6 #include <sys/socket.h>
14 #include "mlx5_utils.h"
17 /* PMD socket service for tools. */
19 #define MLX5_SOCKET_PATH "/var/tmp/dpdk_net_mlx5_%d"
21 int server_socket; /* Unix socket for primary process. */
22 struct rte_intr_handle server_intr_handle; /* Interrupt handler. */
25 * Handle server pmd socket interrupts.
28 mlx5_pmd_socket_handle(void *cb __rte_unused)
32 struct cmsghdr *cmsg = NULL;
34 char buf[CMSG_SPACE(sizeof(int))] = { 0 };
37 .iov_len = sizeof(data),
43 .msg_controllen = sizeof(buf),
48 struct rte_eth_dev *dev;
50 /* Accept the connection from the client. */
51 conn_sock = accept(server_socket, NULL, NULL);
53 DRV_LOG(WARNING, "connection failed: %s", strerror(errno));
56 ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
58 DRV_LOG(WARNING, "wrong message received: %s",
62 /* Receive file descriptor. */
63 cmsg = CMSG_FIRSTHDR(&msg);
64 if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS ||
65 cmsg->cmsg_len < sizeof(int)) {
66 DRV_LOG(WARNING, "invalid file descriptor message");
69 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
70 file = fdopen(fd, "w");
72 DRV_LOG(WARNING, "Failed to open file");
75 /* Receive port number. */
76 if (msg.msg_iovlen != 1 || msg.msg_iov->iov_len < sizeof(uint16_t)) {
77 DRV_LOG(WARNING, "wrong port number message");
80 memcpy(&port_id, msg.msg_iov->iov_base, sizeof(port_id));
81 if (!rte_eth_dev_is_valid_port(port_id)) {
82 DRV_LOG(WARNING, "Invalid port %u", port_id);
86 dev = &rte_eth_devices[port_id];
87 ret = mlx5_flow_dev_dump(dev, NULL, file, NULL);
88 /* Set-up the ancillary data and reply. */
89 msg.msg_controllen = 0;
90 msg.msg_control = NULL;
94 io.iov_len = sizeof(data);
97 ret = sendmsg(conn_sock, &msg, 0);
98 } while (ret < 0 && errno == EINTR);
100 DRV_LOG(WARNING, "failed to send response %s",
110 * Install interrupt handler.
113 * Pointer to Ethernet device.
115 * 0 on success, a negative errno value otherwise.
118 mlx5_pmd_interrupt_handler_install(void)
120 MLX5_ASSERT(server_socket);
121 server_intr_handle.fd = server_socket;
122 server_intr_handle.type = RTE_INTR_HANDLE_EXT;
123 return rte_intr_callback_register(&server_intr_handle,
124 mlx5_pmd_socket_handle, NULL);
128 * Uninstall interrupt handler.
131 mlx5_pmd_interrupt_handler_uninstall(void)
134 mlx5_intr_callback_unregister(&server_intr_handle,
135 mlx5_pmd_socket_handle,
138 server_intr_handle.fd = 0;
139 server_intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
143 * Initialise the socket to communicate with the secondary process
146 * Pointer to Ethernet device.
149 * 0 on success, a negative value otherwise.
152 mlx5_pmd_socket_init(void)
154 struct sockaddr_un sun = {
155 .sun_family = AF_UNIX,
160 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
164 * Initialize the socket to communicate with the secondary
167 ret = socket(AF_UNIX, SOCK_STREAM, 0);
169 DRV_LOG(WARNING, "Failed to open mlx5 socket: %s",
174 flags = fcntl(server_socket, F_GETFL, 0);
177 ret = fcntl(server_socket, F_SETFL, flags | O_NONBLOCK);
180 snprintf(sun.sun_path, sizeof(sun.sun_path), MLX5_SOCKET_PATH,
182 remove(sun.sun_path);
183 ret = bind(server_socket, (const struct sockaddr *)&sun, sizeof(sun));
186 "cannot bind mlx5 socket: %s", strerror(errno));
189 ret = listen(server_socket, 0);
191 DRV_LOG(WARNING, "cannot listen on mlx5 socket: %s",
195 if (mlx5_pmd_interrupt_handler_install()) {
196 DRV_LOG(WARNING, "cannot register interrupt handler for mlx5 socket: %s",
202 remove(sun.sun_path);
204 claim_zero(close(server_socket));
206 DRV_LOG(ERR, "Cannot initialize socket: %s", strerror(errno));
211 * Un-Initialize the pmd socket
213 RTE_FINI(mlx5_pmd_socket_uninit)
217 mlx5_pmd_interrupt_handler_uninstall();
218 claim_zero(close(server_socket));
220 MKSTR(path, MLX5_SOCKET_PATH, getpid());
221 claim_zero(remove(path));