1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
9 #include <sys/socket.h>
17 #include "mlx5_utils.h"
20 * Initialise the socket to communicate with the secondary process
23 * Pointer to Ethernet device.
26 * 0 on success, a negative errno value otherwise and rte_errno is set.
29 mlx5_socket_init(struct rte_eth_dev *dev)
31 struct priv *priv = dev->data->dev_private;
32 struct sockaddr_un sun = {
33 .sun_family = AF_UNIX,
39 * Initialise the socket to communicate with the secondary
42 ret = socket(AF_UNIX, SOCK_STREAM, 0);
45 DRV_LOG(WARNING, "port %u secondary process not supported: %s",
46 dev->data->port_id, strerror(errno));
49 priv->primary_socket = ret;
50 flags = fcntl(priv->primary_socket, F_GETFL, 0);
55 ret = fcntl(priv->primary_socket, F_SETFL, flags | O_NONBLOCK);
60 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
61 MLX5_DRIVER_NAME, priv->primary_socket);
63 ret = bind(priv->primary_socket, (const struct sockaddr *)&sun,
68 "port %u cannot bind socket, secondary process not"
70 dev->data->port_id, strerror(errno));
73 ret = listen(priv->primary_socket, 0);
76 DRV_LOG(WARNING, "port %u secondary process not supported: %s",
77 dev->data->port_id, strerror(errno));
84 claim_zero(close(priv->primary_socket));
85 priv->primary_socket = 0;
90 * Un-Initialise the socket to communicate with the secondary process
95 mlx5_socket_uninit(struct rte_eth_dev *dev)
97 struct priv *priv = dev->data->dev_private;
99 MKSTR(path, "/var/tmp/%s_%d", MLX5_DRIVER_NAME, priv->primary_socket);
100 claim_zero(close(priv->primary_socket));
101 priv->primary_socket = 0;
102 claim_zero(remove(path));
106 * Handle socket interrupts.
109 * Pointer to Ethernet device.
112 mlx5_socket_handle(struct rte_eth_dev *dev)
114 struct priv *priv = dev->data->dev_private;
117 struct cmsghdr *cmsg = NULL;
118 struct ucred *cred = NULL;
119 char buf[CMSG_SPACE(sizeof(struct ucred))] = { 0 };
120 char vbuf[1024] = { 0 };
123 .iov_len = sizeof(*vbuf),
125 struct msghdr msg = {
129 .msg_controllen = sizeof(buf),
133 /* Accept the connection from the client. */
134 conn_sock = accept(priv->primary_socket, NULL, NULL);
136 DRV_LOG(WARNING, "port %u connection failed: %s",
137 dev->data->port_id, strerror(errno));
140 ret = setsockopt(conn_sock, SOL_SOCKET, SO_PASSCRED, &(int){1},
144 DRV_LOG(WARNING, "port %u cannot change socket options: %s",
145 dev->data->port_id, strerror(rte_errno));
148 ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
151 DRV_LOG(WARNING, "port %u received an empty message: %s",
152 dev->data->port_id, strerror(rte_errno));
155 /* Expect to receive credentials only. */
156 cmsg = CMSG_FIRSTHDR(&msg);
158 DRV_LOG(WARNING, "port %u no message", dev->data->port_id);
161 if ((cmsg->cmsg_type == SCM_CREDENTIALS) &&
162 (cmsg->cmsg_len >= sizeof(*cred))) {
163 cred = (struct ucred *)CMSG_DATA(cmsg);
164 assert(cred != NULL);
166 cmsg = CMSG_NXTHDR(&msg, cmsg);
168 DRV_LOG(WARNING, "port %u message wrongly formatted",
172 /* Make sure all the ancillary data was received and valid. */
173 if ((cred == NULL) || (cred->uid != getuid()) ||
174 (cred->gid != getgid())) {
175 DRV_LOG(WARNING, "port %u wrong credentials",
179 /* Set-up the ancillary data. */
180 cmsg = CMSG_FIRSTHDR(&msg);
181 assert(cmsg != NULL);
182 cmsg->cmsg_level = SOL_SOCKET;
183 cmsg->cmsg_type = SCM_RIGHTS;
184 cmsg->cmsg_len = CMSG_LEN(sizeof(priv->ctx->cmd_fd));
185 fd = (int *)CMSG_DATA(cmsg);
186 *fd = priv->ctx->cmd_fd;
187 ret = sendmsg(conn_sock, &msg, 0);
189 DRV_LOG(WARNING, "port %u cannot send response",
196 * Connect to the primary process.
199 * Pointer to Ethernet structure.
202 * fd on success, negative errno value otherwise and rte_errno is set.
205 mlx5_socket_connect(struct rte_eth_dev *dev)
207 struct priv *priv = dev->data->dev_private;
208 struct sockaddr_un sun = {
209 .sun_family = AF_UNIX,
215 char buf[CMSG_SPACE(sizeof(*cred))] = { 0 };
216 char vbuf[1024] = { 0 };
219 .iov_len = sizeof(*vbuf),
221 struct msghdr msg = {
223 .msg_controllen = sizeof(buf),
227 struct cmsghdr *cmsg;
229 ret = socket(AF_UNIX, SOCK_STREAM, 0);
232 DRV_LOG(WARNING, "port %u cannot connect to primary",
237 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
238 MLX5_DRIVER_NAME, priv->primary_socket);
239 ret = connect(socket_fd, (const struct sockaddr *)&sun, sizeof(sun));
242 DRV_LOG(WARNING, "port %u cannot connect to primary",
246 cmsg = CMSG_FIRSTHDR(&msg);
249 DRV_LOG(DEBUG, "port %u cannot get first message",
253 cmsg->cmsg_level = SOL_SOCKET;
254 cmsg->cmsg_type = SCM_CREDENTIALS;
255 cmsg->cmsg_len = CMSG_LEN(sizeof(*cred));
256 cred = (struct ucred *)CMSG_DATA(cmsg);
259 DRV_LOG(DEBUG, "port %u no credentials received",
263 cred->pid = getpid();
264 cred->uid = getuid();
265 cred->gid = getgid();
266 ret = sendmsg(socket_fd, &msg, MSG_DONTWAIT);
270 "port %u cannot send credentials to primary: %s",
271 dev->data->port_id, strerror(errno));
274 ret = recvmsg(socket_fd, &msg, MSG_WAITALL);
277 DRV_LOG(WARNING, "port %u no message from primary: %s",
278 dev->data->port_id, strerror(errno));
281 cmsg = CMSG_FIRSTHDR(&msg);
284 DRV_LOG(WARNING, "port %u no file descriptor received",
288 fd = (int *)CMSG_DATA(cmsg);
290 DRV_LOG(WARNING, "port %u no file descriptor received: %s",
291 dev->data->port_id, strerror(errno));