4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/socket.h>
39 /* sys/un.h with __USE_MISC uses strlen, which is unsafe */
41 #define REMOVED_USE_MISC
45 /* make sure we redefine __USE_MISC only if it was previously undefined */
46 #ifdef REMOVED_USE_MISC
48 #undef REMOVED_USE_MISC
53 #include <rte_eal_memconfig.h>
54 #include <rte_malloc.h>
56 #include "eal_filesystem.h"
57 #include "eal_pci_init.h"
58 #include "eal_thread.h"
62 * VFIO socket for communication between primary and secondary processes.
64 * This file is only compiled if CONFIG_RTE_EAL_VFIO is set to "y".
69 #define SOCKET_PATH_FMT "%s/.%s_mp_socket"
70 #define CMSGLEN (CMSG_LEN(sizeof(int)))
71 #define FD_TO_CMSGHDR(fd, chdr) \
73 (chdr).cmsg_len = CMSGLEN;\
74 (chdr).cmsg_level = SOL_SOCKET;\
75 (chdr).cmsg_type = SCM_RIGHTS;\
76 memcpy((chdr).__cmsg_data, &(fd), sizeof(fd));\
78 #define CMSGHDR_TO_FD(chdr, fd) \
79 memcpy(&(fd), (chdr).__cmsg_data, sizeof(fd))
81 static pthread_t socket_thread;
82 static int mp_socket_fd;
85 /* get socket path (/var/run if root, $HOME otherwise) */
87 get_socket_path(char *buffer, int bufsz)
89 const char *dir = "/var/run";
90 const char *home_dir = getenv("HOME");
92 if (getuid() != 0 && home_dir != NULL)
95 /* use current prefix as file path */
96 snprintf(buffer, bufsz, SOCKET_PATH_FMT, dir,
97 internal_config.hugefile_prefix);
103 * data flow for socket comm protocol:
104 * 1. client sends SOCKET_REQ_CONTAINER or SOCKET_REQ_GROUP
105 * 1a. in case of SOCKET_REQ_GROUP, client also then sends group number
106 * 2. server receives message
107 * 2a. in case of invalid group, SOCKET_ERR is sent back to client
108 * 2b. in case of unbound group, SOCKET_NO_FD is sent back to client
109 * 2c. in case of valid group, SOCKET_OK is sent and immediately followed by fd
111 * in case of any error, socket is closed.
114 /* send a request, return -1 on error */
116 vfio_mp_sync_send_request(int socket, int req)
123 memset(&hdr, 0, sizeof(hdr));
129 iov.iov_base = (char *) &buf;
130 iov.iov_len = sizeof(buf);
132 ret = sendmsg(socket, &hdr, 0);
138 /* receive a request and return it */
140 vfio_mp_sync_receive_request(int socket)
147 memset(&hdr, 0, sizeof(hdr));
153 iov.iov_base = (char *) &buf;
154 iov.iov_len = sizeof(buf);
156 ret = recvmsg(socket, &hdr, 0);
165 /* send OK in message, fd in control message */
167 vfio_mp_sync_send_fd(int socket, int fd)
171 struct cmsghdr *chdr;
172 char chdr_buf[CMSGLEN];
176 chdr = (struct cmsghdr *) chdr_buf;
177 memset(chdr, 0, sizeof(chdr_buf));
178 memset(&hdr, 0, sizeof(hdr));
182 iov.iov_base = (char *) &buf;
183 iov.iov_len = sizeof(buf);
184 hdr.msg_control = chdr;
185 hdr.msg_controllen = CMSGLEN;
188 FD_TO_CMSGHDR(fd, *chdr);
190 ret = sendmsg(socket, &hdr, 0);
196 /* receive OK in message, fd in control message */
198 vfio_mp_sync_receive_fd(int socket)
202 struct cmsghdr *chdr;
203 char chdr_buf[CMSGLEN];
209 chdr = (struct cmsghdr *) chdr_buf;
210 memset(chdr, 0, sizeof(chdr_buf));
211 memset(&hdr, 0, sizeof(hdr));
215 iov.iov_base = (char *) &buf;
216 iov.iov_len = sizeof(buf);
217 hdr.msg_control = chdr;
218 hdr.msg_controllen = CMSGLEN;
220 ret = recvmsg(socket, &hdr, 0);
226 if (req != SOCKET_OK)
229 CMSGHDR_TO_FD(*chdr, fd);
234 /* connect socket_fd in secondary process to the primary process's socket */
236 vfio_mp_sync_connect_to_primary(void)
238 struct sockaddr_un addr;
239 socklen_t sockaddr_len;
242 /* set up a socket */
243 socket_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
245 RTE_LOG(ERR, EAL, "Failed to create socket!\n");
249 get_socket_path(addr.sun_path, sizeof(addr.sun_path));
250 addr.sun_family = AF_UNIX;
252 sockaddr_len = sizeof(struct sockaddr_un);
254 if (connect(socket_fd, (struct sockaddr *) &addr, sockaddr_len) == 0)
257 /* if connect failed */
265 * socket listening thread for primary process
267 static __attribute__((noreturn)) void *
268 vfio_mp_sync_thread(void __rte_unused * arg)
270 int ret, fd, vfio_data;
272 /* wait for requests on the socket */
275 struct sockaddr_un addr;
276 socklen_t sockaddr_len = sizeof(addr);
278 /* this is a blocking call */
279 conn_sock = accept(mp_socket_fd, (struct sockaddr *) &addr,
282 /* just restart on error */
286 /* set socket to linger after close */
291 if (setsockopt(conn_sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0)
292 RTE_LOG(WARNING, EAL, "Cannot set SO_LINGER option "
293 "on listen socket (%s)\n", strerror(errno));
295 ret = vfio_mp_sync_receive_request(conn_sock);
298 case SOCKET_REQ_CONTAINER:
299 fd = vfio_get_container_fd();
301 vfio_mp_sync_send_request(conn_sock, SOCKET_ERR);
303 vfio_mp_sync_send_fd(conn_sock, fd);
307 case SOCKET_REQ_GROUP:
308 /* wait for group number */
309 vfio_data = vfio_mp_sync_receive_request(conn_sock);
315 fd = vfio_get_group_fd(vfio_data);
318 vfio_mp_sync_send_request(conn_sock, SOCKET_ERR);
319 /* if VFIO group exists but isn't bound to VFIO driver */
321 vfio_mp_sync_send_request(conn_sock, SOCKET_NO_FD);
322 /* if group exists and is bound to VFIO driver */
324 vfio_mp_sync_send_request(conn_sock, SOCKET_OK);
325 vfio_mp_sync_send_fd(conn_sock, fd);
328 case SOCKET_CLR_GROUP:
329 /* wait for group fd */
330 vfio_data = vfio_mp_sync_receive_request(conn_sock);
336 ret = clear_group(vfio_data);
339 vfio_mp_sync_send_request(conn_sock, SOCKET_NO_FD);
341 vfio_mp_sync_send_request(conn_sock, SOCKET_OK);
344 vfio_mp_sync_send_request(conn_sock, SOCKET_ERR);
352 vfio_mp_sync_socket_setup(void)
355 struct sockaddr_un addr;
356 socklen_t sockaddr_len;
358 /* set up a socket */
359 socket_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
361 RTE_LOG(ERR, EAL, "Failed to create socket!\n");
365 get_socket_path(addr.sun_path, sizeof(addr.sun_path));
366 addr.sun_family = AF_UNIX;
368 sockaddr_len = sizeof(struct sockaddr_un);
370 unlink(addr.sun_path);
372 ret = bind(socket_fd, (struct sockaddr *) &addr, sockaddr_len);
374 RTE_LOG(ERR, EAL, "Failed to bind socket: %s!\n", strerror(errno));
379 ret = listen(socket_fd, 50);
381 RTE_LOG(ERR, EAL, "Failed to listen: %s!\n", strerror(errno));
386 /* save the socket in local configuration */
387 mp_socket_fd = socket_fd;
393 * set up a local socket and tell it to listen for incoming connections
396 vfio_mp_sync_setup(void)
399 char thread_name[RTE_MAX_THREAD_NAME_LEN];
401 if (vfio_mp_sync_socket_setup() < 0) {
402 RTE_LOG(ERR, EAL, "Failed to set up local socket!\n");
406 ret = pthread_create(&socket_thread, NULL,
407 vfio_mp_sync_thread, NULL);
410 "Failed to create thread for communication with secondary processes!\n");
415 /* Set thread_name for aid in debugging. */
416 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "vfio-sync");
417 ret = rte_thread_setname(socket_thread, thread_name);
420 "Failed to set thread name for secondary processes!\n");