mk: build with _GNU_SOURCE defined by default
[dpdk.git] / drivers / net / mlx5 / mlx5_socket.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox Technologies, Ltd
4  */
5
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <sys/un.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <sys/stat.h>
13
14 #include "mlx5.h"
15 #include "mlx5_utils.h"
16
17 /**
18  * Initialise the socket to communicate with the secondary process
19  *
20  * @param[in] dev
21  *   Pointer to Ethernet device.
22  *
23  * @return
24  *   0 on success, a negative errno value otherwise and rte_errno is set.
25  */
26 int
27 mlx5_socket_init(struct rte_eth_dev *dev)
28 {
29         struct priv *priv = dev->data->dev_private;
30         struct sockaddr_un sun = {
31                 .sun_family = AF_UNIX,
32         };
33         int ret;
34         int flags;
35
36         /*
37          * Close the last socket that was used to communicate
38          * with the secondary process
39          */
40         if (priv->primary_socket)
41                 mlx5_socket_uninit(dev);
42         /*
43          * Initialise the socket to communicate with the secondary
44          * process.
45          */
46         ret = socket(AF_UNIX, SOCK_STREAM, 0);
47         if (ret < 0) {
48                 rte_errno = errno;
49                 DRV_LOG(WARNING, "port %u secondary process not supported: %s",
50                         dev->data->port_id, strerror(errno));
51                 goto error;
52         }
53         priv->primary_socket = ret;
54         flags = fcntl(priv->primary_socket, F_GETFL, 0);
55         if (flags == -1) {
56                 rte_errno = errno;
57                 goto error;
58         }
59         ret = fcntl(priv->primary_socket, F_SETFL, flags | O_NONBLOCK);
60         if (ret < 0) {
61                 rte_errno = errno;
62                 goto error;
63         }
64         snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
65                  MLX5_DRIVER_NAME, priv->primary_socket);
66         remove(sun.sun_path);
67         ret = bind(priv->primary_socket, (const struct sockaddr *)&sun,
68                    sizeof(sun));
69         if (ret < 0) {
70                 rte_errno = errno;
71                 DRV_LOG(WARNING,
72                         "port %u cannot bind socket, secondary process not"
73                         " supported: %s",
74                         dev->data->port_id, strerror(errno));
75                 goto close;
76         }
77         ret = listen(priv->primary_socket, 0);
78         if (ret < 0) {
79                 rte_errno = errno;
80                 DRV_LOG(WARNING, "port %u secondary process not supported: %s",
81                         dev->data->port_id, strerror(errno));
82                 goto close;
83         }
84         return 0;
85 close:
86         remove(sun.sun_path);
87 error:
88         claim_zero(close(priv->primary_socket));
89         priv->primary_socket = 0;
90         return -rte_errno;
91 }
92
93 /**
94  * Un-Initialise the socket to communicate with the secondary process
95  *
96  * @param[in] dev
97  */
98 void
99 mlx5_socket_uninit(struct rte_eth_dev *dev)
100 {
101         struct priv *priv = dev->data->dev_private;
102
103         MKSTR(path, "/var/tmp/%s_%d", MLX5_DRIVER_NAME, priv->primary_socket);
104         claim_zero(close(priv->primary_socket));
105         priv->primary_socket = 0;
106         claim_zero(remove(path));
107 }
108
109 /**
110  * Handle socket interrupts.
111  *
112  * @param dev
113  *   Pointer to Ethernet device.
114  */
115 void
116 mlx5_socket_handle(struct rte_eth_dev *dev)
117 {
118         struct priv *priv = dev->data->dev_private;
119         int conn_sock;
120         int ret = 0;
121         struct cmsghdr *cmsg = NULL;
122         struct ucred *cred = NULL;
123         char buf[CMSG_SPACE(sizeof(struct ucred))] = { 0 };
124         char vbuf[1024] = { 0 };
125         struct iovec io = {
126                 .iov_base = vbuf,
127                 .iov_len = sizeof(*vbuf),
128         };
129         struct msghdr msg = {
130                 .msg_iov = &io,
131                 .msg_iovlen = 1,
132                 .msg_control = buf,
133                 .msg_controllen = sizeof(buf),
134         };
135         int *fd;
136
137         /* Accept the connection from the client. */
138         conn_sock = accept(priv->primary_socket, NULL, NULL);
139         if (conn_sock < 0) {
140                 DRV_LOG(WARNING, "port %u connection failed: %s",
141                         dev->data->port_id, strerror(errno));
142                 return;
143         }
144         ret = setsockopt(conn_sock, SOL_SOCKET, SO_PASSCRED, &(int){1},
145                                          sizeof(int));
146         if (ret < 0) {
147                 ret = errno;
148                 DRV_LOG(WARNING, "port %u cannot change socket options: %s",
149                         dev->data->port_id, strerror(rte_errno));
150                 goto error;
151         }
152         ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
153         if (ret < 0) {
154                 ret = errno;
155                 DRV_LOG(WARNING, "port %u received an empty message: %s",
156                         dev->data->port_id, strerror(rte_errno));
157                 goto error;
158         }
159         /* Expect to receive credentials only. */
160         cmsg = CMSG_FIRSTHDR(&msg);
161         if (cmsg == NULL) {
162                 DRV_LOG(WARNING, "port %u no message", dev->data->port_id);
163                 goto error;
164         }
165         if ((cmsg->cmsg_type == SCM_CREDENTIALS) &&
166                 (cmsg->cmsg_len >= sizeof(*cred))) {
167                 cred = (struct ucred *)CMSG_DATA(cmsg);
168                 assert(cred != NULL);
169         }
170         cmsg = CMSG_NXTHDR(&msg, cmsg);
171         if (cmsg != NULL) {
172                 DRV_LOG(WARNING, "port %u message wrongly formatted",
173                         dev->data->port_id);
174                 goto error;
175         }
176         /* Make sure all the ancillary data was received and valid. */
177         if ((cred == NULL) || (cred->uid != getuid()) ||
178             (cred->gid != getgid())) {
179                 DRV_LOG(WARNING, "port %u wrong credentials",
180                         dev->data->port_id);
181                 goto error;
182         }
183         /* Set-up the ancillary data. */
184         cmsg = CMSG_FIRSTHDR(&msg);
185         assert(cmsg != NULL);
186         cmsg->cmsg_level = SOL_SOCKET;
187         cmsg->cmsg_type = SCM_RIGHTS;
188         cmsg->cmsg_len = CMSG_LEN(sizeof(priv->ctx->cmd_fd));
189         fd = (int *)CMSG_DATA(cmsg);
190         *fd = priv->ctx->cmd_fd;
191         ret = sendmsg(conn_sock, &msg, 0);
192         if (ret < 0)
193                 DRV_LOG(WARNING, "port %u cannot send response",
194                         dev->data->port_id);
195 error:
196         close(conn_sock);
197 }
198
199 /**
200  * Connect to the primary process.
201  *
202  * @param[in] dev
203  *   Pointer to Ethernet structure.
204  *
205  * @return
206  *   fd on success, negative errno value otherwise and rte_errno is set.
207  */
208 int
209 mlx5_socket_connect(struct rte_eth_dev *dev)
210 {
211         struct priv *priv = dev->data->dev_private;
212         struct sockaddr_un sun = {
213                 .sun_family = AF_UNIX,
214         };
215         int socket_fd = -1;
216         int *fd = NULL;
217         int ret;
218         struct ucred *cred;
219         char buf[CMSG_SPACE(sizeof(*cred))] = { 0 };
220         char vbuf[1024] = { 0 };
221         struct iovec io = {
222                 .iov_base = vbuf,
223                 .iov_len = sizeof(*vbuf),
224         };
225         struct msghdr msg = {
226                 .msg_control = buf,
227                 .msg_controllen = sizeof(buf),
228                 .msg_iov = &io,
229                 .msg_iovlen = 1,
230         };
231         struct cmsghdr *cmsg;
232
233         ret = socket(AF_UNIX, SOCK_STREAM, 0);
234         if (ret < 0) {
235                 rte_errno = errno;
236                 DRV_LOG(WARNING, "port %u cannot connect to primary",
237                         dev->data->port_id);
238                 goto error;
239         }
240         socket_fd = ret;
241         snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
242                  MLX5_DRIVER_NAME, priv->primary_socket);
243         ret = connect(socket_fd, (const struct sockaddr *)&sun, sizeof(sun));
244         if (ret < 0) {
245                 rte_errno = errno;
246                 DRV_LOG(WARNING, "port %u cannot connect to primary",
247                         dev->data->port_id);
248                 goto error;
249         }
250         cmsg = CMSG_FIRSTHDR(&msg);
251         if (cmsg == NULL) {
252                 rte_errno = EINVAL;
253                 DRV_LOG(DEBUG, "port %u cannot get first message",
254                         dev->data->port_id);
255                 goto error;
256         }
257         cmsg->cmsg_level = SOL_SOCKET;
258         cmsg->cmsg_type = SCM_CREDENTIALS;
259         cmsg->cmsg_len = CMSG_LEN(sizeof(*cred));
260         cred = (struct ucred *)CMSG_DATA(cmsg);
261         if (cred == NULL) {
262                 rte_errno = EINVAL;
263                 DRV_LOG(DEBUG, "port %u no credentials received",
264                         dev->data->port_id);
265                 goto error;
266         }
267         cred->pid = getpid();
268         cred->uid = getuid();
269         cred->gid = getgid();
270         ret = sendmsg(socket_fd, &msg, MSG_DONTWAIT);
271         if (ret < 0) {
272                 rte_errno = errno;
273                 DRV_LOG(WARNING,
274                         "port %u cannot send credentials to primary: %s",
275                         dev->data->port_id, strerror(errno));
276                 goto error;
277         }
278         ret = recvmsg(socket_fd, &msg, MSG_WAITALL);
279         if (ret <= 0) {
280                 rte_errno = errno;
281                 DRV_LOG(WARNING, "port %u no message from primary: %s",
282                         dev->data->port_id, strerror(errno));
283                 goto error;
284         }
285         cmsg = CMSG_FIRSTHDR(&msg);
286         if (cmsg == NULL) {
287                 rte_errno = EINVAL;
288                 DRV_LOG(WARNING, "port %u no file descriptor received",
289                         dev->data->port_id);
290                 goto error;
291         }
292         fd = (int *)CMSG_DATA(cmsg);
293         if (*fd < 0) {
294                 DRV_LOG(WARNING, "port %u no file descriptor received: %s",
295                         dev->data->port_id, strerror(errno));
296                 rte_errno = *fd;
297                 goto error;
298         }
299         ret = *fd;
300         close(socket_fd);
301         return ret;
302 error:
303         if (socket_fd != -1)
304                 close(socket_fd);
305         return -rte_errno;
306 }