4 * Copyright 2016 6WIND S.A.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of 6WIND S.A. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <sys/socket.h>
43 #include "mlx5_utils.h"
46 * Initialise the socket to communicate with the secondary process
49 * Pointer to Ethernet device.
52 * 0 on success, a negative errno value otherwise and rte_errno is set.
55 mlx5_socket_init(struct rte_eth_dev *dev)
57 struct priv *priv = dev->data->dev_private;
58 struct sockaddr_un sun = {
59 .sun_family = AF_UNIX,
65 * Close the last socket that was used to communicate
66 * with the secondary process
68 if (priv->primary_socket)
69 mlx5_socket_uninit(dev);
71 * Initialise the socket to communicate with the secondary
74 ret = socket(AF_UNIX, SOCK_STREAM, 0);
77 DRV_LOG(WARNING, "port %u secondary process not supported: %s",
78 dev->data->port_id, strerror(errno));
81 priv->primary_socket = ret;
82 flags = fcntl(priv->primary_socket, F_GETFL, 0);
87 ret = fcntl(priv->primary_socket, F_SETFL, flags | O_NONBLOCK);
92 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
93 MLX5_DRIVER_NAME, priv->primary_socket);
95 ret = bind(priv->primary_socket, (const struct sockaddr *)&sun,
100 "port %u cannot bind socket, secondary process not"
102 dev->data->port_id, strerror(errno));
105 ret = listen(priv->primary_socket, 0);
108 DRV_LOG(WARNING, "port %u secondary process not supported: %s",
109 dev->data->port_id, strerror(errno));
114 remove(sun.sun_path);
116 claim_zero(close(priv->primary_socket));
117 priv->primary_socket = 0;
122 * Un-Initialise the socket to communicate with the secondary process
127 mlx5_socket_uninit(struct rte_eth_dev *dev)
129 struct priv *priv = dev->data->dev_private;
131 MKSTR(path, "/var/tmp/%s_%d", MLX5_DRIVER_NAME, priv->primary_socket);
132 claim_zero(close(priv->primary_socket));
133 priv->primary_socket = 0;
134 claim_zero(remove(path));
138 * Handle socket interrupts.
141 * Pointer to Ethernet device.
144 mlx5_socket_handle(struct rte_eth_dev *dev)
146 struct priv *priv = dev->data->dev_private;
149 struct cmsghdr *cmsg = NULL;
150 struct ucred *cred = NULL;
151 char buf[CMSG_SPACE(sizeof(struct ucred))] = { 0 };
152 char vbuf[1024] = { 0 };
155 .iov_len = sizeof(*vbuf),
157 struct msghdr msg = {
161 .msg_controllen = sizeof(buf),
165 /* Accept the connection from the client. */
166 conn_sock = accept(priv->primary_socket, NULL, NULL);
168 DRV_LOG(WARNING, "port %u connection failed: %s",
169 dev->data->port_id, strerror(errno));
172 ret = setsockopt(conn_sock, SOL_SOCKET, SO_PASSCRED, &(int){1},
176 DRV_LOG(WARNING, "port %u cannot change socket options: %s",
177 dev->data->port_id, strerror(rte_errno));
180 ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
183 DRV_LOG(WARNING, "port %u received an empty message: %s",
184 dev->data->port_id, strerror(rte_errno));
187 /* Expect to receive credentials only. */
188 cmsg = CMSG_FIRSTHDR(&msg);
190 DRV_LOG(WARNING, "port %u no message", dev->data->port_id);
193 if ((cmsg->cmsg_type == SCM_CREDENTIALS) &&
194 (cmsg->cmsg_len >= sizeof(*cred))) {
195 cred = (struct ucred *)CMSG_DATA(cmsg);
196 assert(cred != NULL);
198 cmsg = CMSG_NXTHDR(&msg, cmsg);
200 DRV_LOG(WARNING, "port %u message wrongly formatted",
204 /* Make sure all the ancillary data was received and valid. */
205 if ((cred == NULL) || (cred->uid != getuid()) ||
206 (cred->gid != getgid())) {
207 DRV_LOG(WARNING, "port %u wrong credentials",
211 /* Set-up the ancillary data. */
212 cmsg = CMSG_FIRSTHDR(&msg);
213 assert(cmsg != NULL);
214 cmsg->cmsg_level = SOL_SOCKET;
215 cmsg->cmsg_type = SCM_RIGHTS;
216 cmsg->cmsg_len = CMSG_LEN(sizeof(priv->ctx->cmd_fd));
217 fd = (int *)CMSG_DATA(cmsg);
218 *fd = priv->ctx->cmd_fd;
219 ret = sendmsg(conn_sock, &msg, 0);
221 DRV_LOG(WARNING, "port %u cannot send response",
228 * Connect to the primary process.
231 * Pointer to Ethernet structure.
234 * fd on success, negative errno value otherwise and rte_errno is set.
237 mlx5_socket_connect(struct rte_eth_dev *dev)
239 struct priv *priv = dev->data->dev_private;
240 struct sockaddr_un sun = {
241 .sun_family = AF_UNIX,
247 char buf[CMSG_SPACE(sizeof(*cred))] = { 0 };
248 char vbuf[1024] = { 0 };
251 .iov_len = sizeof(*vbuf),
253 struct msghdr msg = {
255 .msg_controllen = sizeof(buf),
259 struct cmsghdr *cmsg;
261 ret = socket(AF_UNIX, SOCK_STREAM, 0);
264 DRV_LOG(WARNING, "port %u cannot connect to primary",
269 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
270 MLX5_DRIVER_NAME, priv->primary_socket);
271 ret = connect(socket_fd, (const struct sockaddr *)&sun, sizeof(sun));
274 DRV_LOG(WARNING, "port %u cannot connect to primary",
278 cmsg = CMSG_FIRSTHDR(&msg);
281 DRV_LOG(DEBUG, "port %u cannot get first message",
285 cmsg->cmsg_level = SOL_SOCKET;
286 cmsg->cmsg_type = SCM_CREDENTIALS;
287 cmsg->cmsg_len = CMSG_LEN(sizeof(*cred));
288 cred = (struct ucred *)CMSG_DATA(cmsg);
291 DRV_LOG(DEBUG, "port %u no credentials received",
295 cred->pid = getpid();
296 cred->uid = getuid();
297 cred->gid = getgid();
298 ret = sendmsg(socket_fd, &msg, MSG_DONTWAIT);
302 "port %u cannot send credentials to primary: %s",
303 dev->data->port_id, strerror(errno));
306 ret = recvmsg(socket_fd, &msg, MSG_WAITALL);
309 DRV_LOG(WARNING, "port %u no message from primary: %s",
310 dev->data->port_id, strerror(errno));
313 cmsg = CMSG_FIRSTHDR(&msg);
316 DRV_LOG(WARNING, "port %u no file descriptor received",
320 fd = (int *)CMSG_DATA(cmsg);
322 DRV_LOG(WARNING, "port %u no file descriptor received: %s",
323 dev->data->port_id, strerror(errno));