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 pci_vfio_mp_sync_thread(void __rte_unused * arg)
270 int ret, fd, vfio_group_no;
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 */
290 setsockopt(conn_sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
292 ret = vfio_mp_sync_receive_request(conn_sock);
295 case SOCKET_REQ_CONTAINER:
296 fd = pci_vfio_get_container_fd();
298 vfio_mp_sync_send_request(conn_sock, SOCKET_ERR);
300 vfio_mp_sync_send_fd(conn_sock, fd);
302 case SOCKET_REQ_GROUP:
303 /* wait for group number */
304 vfio_group_no = vfio_mp_sync_receive_request(conn_sock);
305 if (vfio_group_no < 0) {
310 fd = pci_vfio_get_group_fd(vfio_group_no);
313 vfio_mp_sync_send_request(conn_sock, SOCKET_ERR);
314 /* if VFIO group exists but isn't bound to VFIO driver */
316 vfio_mp_sync_send_request(conn_sock, SOCKET_NO_FD);
317 /* if group exists and is bound to VFIO driver */
319 vfio_mp_sync_send_request(conn_sock, SOCKET_OK);
320 vfio_mp_sync_send_fd(conn_sock, fd);
324 vfio_mp_sync_send_request(conn_sock, SOCKET_ERR);
332 vfio_mp_sync_socket_setup(void)
335 struct sockaddr_un addr;
336 socklen_t sockaddr_len;
338 /* set up a socket */
339 socket_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
341 RTE_LOG(ERR, EAL, "Failed to create socket!\n");
345 get_socket_path(addr.sun_path, sizeof(addr.sun_path));
346 addr.sun_family = AF_UNIX;
348 sockaddr_len = sizeof(struct sockaddr_un);
350 unlink(addr.sun_path);
352 ret = bind(socket_fd, (struct sockaddr *) &addr, sockaddr_len);
354 RTE_LOG(ERR, EAL, "Failed to bind socket: %s!\n", strerror(errno));
359 ret = listen(socket_fd, 50);
361 RTE_LOG(ERR, EAL, "Failed to listen: %s!\n", strerror(errno));
366 /* save the socket in local configuration */
367 mp_socket_fd = socket_fd;
373 * set up a local socket and tell it to listen for incoming connections
376 pci_vfio_mp_sync_setup(void)
379 char thread_name[RTE_MAX_THREAD_NAME_LEN];
381 if (vfio_mp_sync_socket_setup() < 0) {
382 RTE_LOG(ERR, EAL, "Failed to set up local socket!\n");
386 ret = pthread_create(&socket_thread, NULL,
387 pci_vfio_mp_sync_thread, NULL);
390 "Failed to create thread for communication with secondary processes!\n");
395 /* Set thread_name for aid in debugging. */
396 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "pci-vfio-sync");
397 ret = rte_thread_setname(socket_thread, thread_name);
400 "Failed to set thread name for secondary processes!\n");