1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
8 #include <sys/socket.h>
16 #include "mlx5_utils.h"
19 * Initialise the socket to communicate with the secondary process
22 * Pointer to Ethernet device.
25 * 0 on success, errno value on failure.
28 mlx5_socket_init(struct rte_eth_dev *dev)
30 struct priv *priv = dev->data->dev_private;
31 struct sockaddr_un sun = {
32 .sun_family = AF_UNIX,
36 struct stat file_stat;
39 * Initialise the socket to communicate with the secondary
42 ret = socket(AF_UNIX, SOCK_STREAM, 0);
44 WARN("secondary process not supported: %s", strerror(errno));
47 priv->primary_socket = ret;
48 flags = fcntl(priv->primary_socket, F_GETFL, 0);
51 ret = fcntl(priv->primary_socket, F_SETFL, flags | O_NONBLOCK);
54 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
55 MLX5_DRIVER_NAME, priv->primary_socket);
56 ret = stat(sun.sun_path, &file_stat);
58 claim_zero(remove(sun.sun_path));
59 ret = bind(priv->primary_socket, (const struct sockaddr *)&sun,
62 WARN("cannot bind socket, secondary process not supported: %s",
66 ret = listen(priv->primary_socket, 0);
68 WARN("Secondary process not supported: %s", strerror(errno));
75 claim_zero(close(priv->primary_socket));
76 priv->primary_socket = 0;
81 * Un-Initialise the socket to communicate with the secondary process
84 * Pointer to Ethernet device.
87 mlx5_socket_uninit(struct rte_eth_dev *dev)
89 struct priv *priv = dev->data->dev_private;
91 MKSTR(path, "/var/tmp/%s_%d", MLX5_DRIVER_NAME, priv->primary_socket);
92 claim_zero(close(priv->primary_socket));
93 priv->primary_socket = 0;
94 claim_zero(remove(path));
98 * Handle socket interrupts.
101 * Pointer to Ethernet device.
104 mlx5_socket_handle(struct rte_eth_dev *dev)
106 struct priv *priv = dev->data->dev_private;
109 struct cmsghdr *cmsg = NULL;
110 struct ucred *cred = NULL;
111 char buf[CMSG_SPACE(sizeof(struct ucred))] = { 0 };
112 char vbuf[1024] = { 0 };
115 .iov_len = sizeof(*vbuf),
117 struct msghdr msg = {
121 .msg_controllen = sizeof(buf),
125 /* Accept the connection from the client. */
126 conn_sock = accept(priv->primary_socket, NULL, NULL);
128 WARN("connection failed: %s", strerror(errno));
131 ret = setsockopt(conn_sock, SOL_SOCKET, SO_PASSCRED, &(int){1},
134 WARN("cannot change socket options");
137 ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
139 WARN("received an empty message: %s", strerror(errno));
142 /* Expect to receive credentials only. */
143 cmsg = CMSG_FIRSTHDR(&msg);
148 if ((cmsg->cmsg_type == SCM_CREDENTIALS) &&
149 (cmsg->cmsg_len >= sizeof(*cred))) {
150 cred = (struct ucred *)CMSG_DATA(cmsg);
151 assert(cred != NULL);
153 cmsg = CMSG_NXTHDR(&msg, cmsg);
155 WARN("Message wrongly formatted");
158 /* Make sure all the ancillary data was received and valid. */
159 if ((cred == NULL) || (cred->uid != getuid()) ||
160 (cred->gid != getgid())) {
161 WARN("wrong credentials");
164 /* Set-up the ancillary data. */
165 cmsg = CMSG_FIRSTHDR(&msg);
166 assert(cmsg != NULL);
167 cmsg->cmsg_level = SOL_SOCKET;
168 cmsg->cmsg_type = SCM_RIGHTS;
169 cmsg->cmsg_len = CMSG_LEN(sizeof(priv->ctx->cmd_fd));
170 fd = (int *)CMSG_DATA(cmsg);
171 *fd = priv->ctx->cmd_fd;
172 ret = sendmsg(conn_sock, &msg, 0);
174 WARN("cannot send response");
180 * Connect to the primary process.
183 * Pointer to Ethernet structure.
186 * fd on success, negative errno value on failure.
189 mlx5_socket_connect(struct rte_eth_dev *dev)
191 struct priv *priv = dev->data->dev_private;
192 struct sockaddr_un sun = {
193 .sun_family = AF_UNIX,
199 char buf[CMSG_SPACE(sizeof(*cred))] = { 0 };
200 char vbuf[1024] = { 0 };
203 .iov_len = sizeof(*vbuf),
205 struct msghdr msg = {
207 .msg_controllen = sizeof(buf),
211 struct cmsghdr *cmsg;
213 ret = socket(AF_UNIX, SOCK_STREAM, 0);
215 WARN("cannot connect to primary");
219 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
220 MLX5_DRIVER_NAME, priv->primary_socket);
221 ret = connect(socket_fd, (const struct sockaddr *)&sun, sizeof(sun));
223 WARN("cannot connect to primary");
226 cmsg = CMSG_FIRSTHDR(&msg);
228 DEBUG("cannot get first message");
231 cmsg->cmsg_level = SOL_SOCKET;
232 cmsg->cmsg_type = SCM_CREDENTIALS;
233 cmsg->cmsg_len = CMSG_LEN(sizeof(*cred));
234 cred = (struct ucred *)CMSG_DATA(cmsg);
236 DEBUG("no credentials received");
239 cred->pid = getpid();
240 cred->uid = getuid();
241 cred->gid = getgid();
242 ret = sendmsg(socket_fd, &msg, MSG_DONTWAIT);
244 WARN("cannot send credentials to primary: %s",
248 ret = recvmsg(socket_fd, &msg, MSG_WAITALL);
250 WARN("no message from primary: %s", strerror(errno));
253 cmsg = CMSG_FIRSTHDR(&msg);
255 WARN("No file descriptor received");
258 fd = (int *)CMSG_DATA(cmsg);
260 WARN("no file descriptor received: %s", strerror(errno));