1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
10 #include <sys/socket.h>
18 #include "mlx5_utils.h"
21 /* PMD socket service for tools. */
23 #define MLX5_SOCKET_PATH "/var/tmp/dpdk_net_mlx5_%d"
25 int server_socket; /* Unix socket for primary process. */
26 struct rte_intr_handle server_intr_handle; /* Interrupt handler. */
29 * Handle server pmd socket interrupts.
32 mlx5_pmd_socket_handle(void *cb __rte_unused)
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 };
42 .iov_len = sizeof(data),
48 .msg_controllen = sizeof(buf),
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;
59 memset(data, 0, sizeof(data));
60 /* Accept the connection from the client. */
61 conn_sock = accept(server_socket, NULL, NULL);
63 DRV_LOG(WARNING, "connection failed: %s", strerror(errno));
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",
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");
80 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
81 file = fdopen(fd, "w");
83 DRV_LOG(WARNING, "Failed to open file");
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");
92 dump_req = (struct mlx5_flow_dump_req *)msg.msg_iov->iov_base;
94 port_id = dump_req->port_id;
95 flow_ptr = dump_req->flow_id;
97 DRV_LOG(WARNING, "Invalid message");
101 if (!rte_eth_dev_is_valid_port(port_id)) {
102 DRV_LOG(WARNING, "Invalid port %u", port_id);
107 dev = &rte_eth_devices[port_id];
109 ret = mlx5_flow_dev_dump(dev, NULL, file, NULL);
111 ret = mlx5_flow_dev_dump(dev,
112 (struct rte_flow *)((uintptr_t)flow_ptr), file, &err);
114 /* Set-up the ancillary data and reply. */
115 msg.msg_controllen = 0;
116 msg.msg_control = NULL;
119 dump_ack = (struct mlx5_flow_dump_ack *)data;
121 io.iov_len = sizeof(struct mlx5_flow_dump_ack);
122 io.iov_base = dump_ack;
124 ret = sendmsg(conn_sock, &msg, 0);
125 } while (ret < 0 && errno == EINTR);
127 DRV_LOG(WARNING, "failed to send response %s",
137 * Install interrupt handler.
140 * Pointer to Ethernet device.
142 * 0 on success, a negative errno value otherwise.
145 mlx5_pmd_interrupt_handler_install(void)
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);
155 * Uninstall interrupt handler.
158 mlx5_pmd_interrupt_handler_uninstall(void)
161 mlx5_intr_callback_unregister(&server_intr_handle,
162 mlx5_pmd_socket_handle,
165 server_intr_handle.fd = 0;
166 server_intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
170 * Initialise the socket to communicate with the secondary process
173 * Pointer to Ethernet device.
176 * 0 on success, a negative value otherwise.
179 mlx5_pmd_socket_init(void)
181 struct sockaddr_un sun = {
182 .sun_family = AF_UNIX,
187 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
191 * Initialize the socket to communicate with the secondary
194 ret = socket(AF_UNIX, SOCK_STREAM, 0);
196 DRV_LOG(WARNING, "Failed to open mlx5 socket: %s",
201 flags = fcntl(server_socket, F_GETFL, 0);
204 ret = fcntl(server_socket, F_SETFL, flags | O_NONBLOCK);
207 snprintf(sun.sun_path, sizeof(sun.sun_path), MLX5_SOCKET_PATH,
209 remove(sun.sun_path);
210 ret = bind(server_socket, (const struct sockaddr *)&sun, sizeof(sun));
213 "cannot bind mlx5 socket: %s", strerror(errno));
216 ret = listen(server_socket, 0);
218 DRV_LOG(WARNING, "cannot listen on mlx5 socket: %s",
222 if (mlx5_pmd_interrupt_handler_install()) {
223 DRV_LOG(WARNING, "cannot register interrupt handler for mlx5 socket: %s",
229 remove(sun.sun_path);
231 claim_zero(close(server_socket));
233 DRV_LOG(ERR, "Cannot initialize socket: %s", strerror(errno));
238 * Un-Initialize the pmd socket
240 RTE_FINI(mlx5_pmd_socket_uninit)
244 mlx5_pmd_interrupt_handler_uninstall();
245 claim_zero(close(server_socket));
247 MKSTR(path, MLX5_SOCKET_PATH, getpid());
248 claim_zero(remove(path));