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 * Initialise the socket to communicate with the secondary
68 ret = socket(AF_UNIX, SOCK_STREAM, 0);
71 DRV_LOG(WARNING, "port %u secondary process not supported: %s",
72 dev->data->port_id, strerror(errno));
75 priv->primary_socket = ret;
76 flags = fcntl(priv->primary_socket, F_GETFL, 0);
81 ret = fcntl(priv->primary_socket, F_SETFL, flags | O_NONBLOCK);
86 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
87 MLX5_DRIVER_NAME, priv->primary_socket);
89 ret = bind(priv->primary_socket, (const struct sockaddr *)&sun,
94 "port %u cannot bind socket, secondary process not"
96 dev->data->port_id, strerror(errno));
99 ret = listen(priv->primary_socket, 0);
102 DRV_LOG(WARNING, "port %u secondary process not supported: %s",
103 dev->data->port_id, strerror(errno));
108 remove(sun.sun_path);
110 claim_zero(close(priv->primary_socket));
111 priv->primary_socket = 0;
116 * Un-Initialise the socket to communicate with the secondary process
121 mlx5_socket_uninit(struct rte_eth_dev *dev)
123 struct priv *priv = dev->data->dev_private;
125 MKSTR(path, "/var/tmp/%s_%d", MLX5_DRIVER_NAME, priv->primary_socket);
126 claim_zero(close(priv->primary_socket));
127 priv->primary_socket = 0;
128 claim_zero(remove(path));
132 * Handle socket interrupts.
135 * Pointer to Ethernet device.
138 mlx5_socket_handle(struct rte_eth_dev *dev)
140 struct priv *priv = dev->data->dev_private;
143 struct cmsghdr *cmsg = NULL;
144 struct ucred *cred = NULL;
145 char buf[CMSG_SPACE(sizeof(struct ucred))] = { 0 };
146 char vbuf[1024] = { 0 };
149 .iov_len = sizeof(*vbuf),
151 struct msghdr msg = {
155 .msg_controllen = sizeof(buf),
159 /* Accept the connection from the client. */
160 conn_sock = accept(priv->primary_socket, NULL, NULL);
162 DRV_LOG(WARNING, "port %u connection failed: %s",
163 dev->data->port_id, strerror(errno));
166 ret = setsockopt(conn_sock, SOL_SOCKET, SO_PASSCRED, &(int){1},
170 DRV_LOG(WARNING, "port %u cannot change socket options: %s",
171 dev->data->port_id, strerror(rte_errno));
174 ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
177 DRV_LOG(WARNING, "port %u received an empty message: %s",
178 dev->data->port_id, strerror(rte_errno));
181 /* Expect to receive credentials only. */
182 cmsg = CMSG_FIRSTHDR(&msg);
184 DRV_LOG(WARNING, "port %u no message", dev->data->port_id);
187 if ((cmsg->cmsg_type == SCM_CREDENTIALS) &&
188 (cmsg->cmsg_len >= sizeof(*cred))) {
189 cred = (struct ucred *)CMSG_DATA(cmsg);
190 assert(cred != NULL);
192 cmsg = CMSG_NXTHDR(&msg, cmsg);
194 DRV_LOG(WARNING, "port %u message wrongly formatted",
198 /* Make sure all the ancillary data was received and valid. */
199 if ((cred == NULL) || (cred->uid != getuid()) ||
200 (cred->gid != getgid())) {
201 DRV_LOG(WARNING, "port %u wrong credentials",
205 /* Set-up the ancillary data. */
206 cmsg = CMSG_FIRSTHDR(&msg);
207 assert(cmsg != NULL);
208 cmsg->cmsg_level = SOL_SOCKET;
209 cmsg->cmsg_type = SCM_RIGHTS;
210 cmsg->cmsg_len = CMSG_LEN(sizeof(priv->ctx->cmd_fd));
211 fd = (int *)CMSG_DATA(cmsg);
212 *fd = priv->ctx->cmd_fd;
213 ret = sendmsg(conn_sock, &msg, 0);
215 DRV_LOG(WARNING, "port %u cannot send response",
222 * Connect to the primary process.
225 * Pointer to Ethernet structure.
228 * fd on success, negative errno value otherwise and rte_errno is set.
231 mlx5_socket_connect(struct rte_eth_dev *dev)
233 struct priv *priv = dev->data->dev_private;
234 struct sockaddr_un sun = {
235 .sun_family = AF_UNIX,
241 char buf[CMSG_SPACE(sizeof(*cred))] = { 0 };
242 char vbuf[1024] = { 0 };
245 .iov_len = sizeof(*vbuf),
247 struct msghdr msg = {
249 .msg_controllen = sizeof(buf),
253 struct cmsghdr *cmsg;
255 ret = socket(AF_UNIX, SOCK_STREAM, 0);
258 DRV_LOG(WARNING, "port %u cannot connect to primary",
263 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
264 MLX5_DRIVER_NAME, priv->primary_socket);
265 ret = connect(socket_fd, (const struct sockaddr *)&sun, sizeof(sun));
268 DRV_LOG(WARNING, "port %u cannot connect to primary",
272 cmsg = CMSG_FIRSTHDR(&msg);
275 DRV_LOG(DEBUG, "port %u cannot get first message",
279 cmsg->cmsg_level = SOL_SOCKET;
280 cmsg->cmsg_type = SCM_CREDENTIALS;
281 cmsg->cmsg_len = CMSG_LEN(sizeof(*cred));
282 cred = (struct ucred *)CMSG_DATA(cmsg);
285 DRV_LOG(DEBUG, "port %u no credentials received",
289 cred->pid = getpid();
290 cred->uid = getuid();
291 cred->gid = getgid();
292 ret = sendmsg(socket_fd, &msg, MSG_DONTWAIT);
296 "port %u cannot send credentials to primary: %s",
297 dev->data->port_id, strerror(errno));
300 ret = recvmsg(socket_fd, &msg, MSG_WAITALL);
303 DRV_LOG(WARNING, "port %u no message from primary: %s",
304 dev->data->port_id, strerror(errno));
307 cmsg = CMSG_FIRSTHDR(&msg);
310 DRV_LOG(WARNING, "port %u no file descriptor received",
314 fd = (int *)CMSG_DATA(cmsg);
316 DRV_LOG(WARNING, "port %u no file descriptor received: %s",
317 dev->data->port_id, strerror(errno));