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, a negative errno value otherwise and rte_errno is set.
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);
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);
62 ret = stat(sun.sun_path, &file_stat);
64 claim_zero(remove(sun.sun_path));
65 ret = bind(priv->primary_socket, (const struct sockaddr *)&sun,
70 "port %u cannot bind socket, secondary process not"
72 dev->data->port_id, strerror(errno));
75 ret = listen(priv->primary_socket, 0);
78 DRV_LOG(WARNING, "port %u secondary process not supported: %s",
79 dev->data->port_id, strerror(errno));
86 claim_zero(close(priv->primary_socket));
87 priv->primary_socket = 0;
92 * Un-Initialise the socket to communicate with the secondary process
97 mlx5_socket_uninit(struct rte_eth_dev *dev)
99 struct priv *priv = dev->data->dev_private;
101 MKSTR(path, "/var/tmp/%s_%d", MLX5_DRIVER_NAME, priv->primary_socket);
102 claim_zero(close(priv->primary_socket));
103 priv->primary_socket = 0;
104 claim_zero(remove(path));
108 * Handle socket interrupts.
111 * Pointer to Ethernet device.
114 mlx5_socket_handle(struct rte_eth_dev *dev)
116 struct priv *priv = dev->data->dev_private;
119 struct cmsghdr *cmsg = NULL;
120 struct ucred *cred = NULL;
121 char buf[CMSG_SPACE(sizeof(struct ucred))] = { 0 };
122 char vbuf[1024] = { 0 };
125 .iov_len = sizeof(*vbuf),
127 struct msghdr msg = {
131 .msg_controllen = sizeof(buf),
135 /* Accept the connection from the client. */
136 conn_sock = accept(priv->primary_socket, NULL, NULL);
138 DRV_LOG(WARNING, "port %u connection failed: %s",
139 dev->data->port_id, strerror(errno));
142 ret = setsockopt(conn_sock, SOL_SOCKET, SO_PASSCRED, &(int){1},
146 DRV_LOG(WARNING, "port %u cannot change socket options: %s",
147 dev->data->port_id, strerror(rte_errno));
150 ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
153 DRV_LOG(WARNING, "port %u received an empty message: %s",
154 dev->data->port_id, strerror(rte_errno));
157 /* Expect to receive credentials only. */
158 cmsg = CMSG_FIRSTHDR(&msg);
160 DRV_LOG(WARNING, "port %u no message", dev->data->port_id);
163 if ((cmsg->cmsg_type == SCM_CREDENTIALS) &&
164 (cmsg->cmsg_len >= sizeof(*cred))) {
165 cred = (struct ucred *)CMSG_DATA(cmsg);
166 assert(cred != NULL);
168 cmsg = CMSG_NXTHDR(&msg, cmsg);
170 DRV_LOG(WARNING, "port %u message wrongly formatted",
174 /* Make sure all the ancillary data was received and valid. */
175 if ((cred == NULL) || (cred->uid != getuid()) ||
176 (cred->gid != getgid())) {
177 DRV_LOG(WARNING, "port %u wrong credentials",
181 /* Set-up the ancillary data. */
182 cmsg = CMSG_FIRSTHDR(&msg);
183 assert(cmsg != NULL);
184 cmsg->cmsg_level = SOL_SOCKET;
185 cmsg->cmsg_type = SCM_RIGHTS;
186 cmsg->cmsg_len = CMSG_LEN(sizeof(priv->ctx->cmd_fd));
187 fd = (int *)CMSG_DATA(cmsg);
188 *fd = priv->ctx->cmd_fd;
189 ret = sendmsg(conn_sock, &msg, 0);
191 DRV_LOG(WARNING, "port %u cannot send response",
198 * Connect to the primary process.
201 * Pointer to Ethernet structure.
204 * fd on success, negative errno value otherwise and rte_errno is set.
207 mlx5_socket_connect(struct rte_eth_dev *dev)
209 struct priv *priv = dev->data->dev_private;
210 struct sockaddr_un sun = {
211 .sun_family = AF_UNIX,
217 char buf[CMSG_SPACE(sizeof(*cred))] = { 0 };
218 char vbuf[1024] = { 0 };
221 .iov_len = sizeof(*vbuf),
223 struct msghdr msg = {
225 .msg_controllen = sizeof(buf),
229 struct cmsghdr *cmsg;
231 ret = socket(AF_UNIX, SOCK_STREAM, 0);
234 DRV_LOG(WARNING, "port %u cannot connect to primary",
239 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
240 MLX5_DRIVER_NAME, priv->primary_socket);
241 ret = connect(socket_fd, (const struct sockaddr *)&sun, sizeof(sun));
244 DRV_LOG(WARNING, "port %u cannot connect to primary",
248 cmsg = CMSG_FIRSTHDR(&msg);
251 DRV_LOG(DEBUG, "port %u cannot get first message",
255 cmsg->cmsg_level = SOL_SOCKET;
256 cmsg->cmsg_type = SCM_CREDENTIALS;
257 cmsg->cmsg_len = CMSG_LEN(sizeof(*cred));
258 cred = (struct ucred *)CMSG_DATA(cmsg);
261 DRV_LOG(DEBUG, "port %u no credentials received",
265 cred->pid = getpid();
266 cred->uid = getuid();
267 cred->gid = getgid();
268 ret = sendmsg(socket_fd, &msg, MSG_DONTWAIT);
272 "port %u cannot send credentials to primary: %s",
273 dev->data->port_id, strerror(errno));
276 ret = recvmsg(socket_fd, &msg, MSG_WAITALL);
279 DRV_LOG(WARNING, "port %u no message from primary: %s",
280 dev->data->port_id, strerror(errno));
283 cmsg = CMSG_FIRSTHDR(&msg);
286 DRV_LOG(WARNING, "port %u no file descriptor received",
290 fd = (int *)CMSG_DATA(cmsg);
292 DRV_LOG(WARNING, "port %u no file descriptor received: %s",
293 dev->data->port_id, strerror(errno));