xref: /qemu/contrib/ivshmem-server/ivshmem-server.c (revision ccd241b5a2223c502441714d413d569565e4a3b4)
1a75eb03bSDavid Marchand /*
2a75eb03bSDavid Marchand  * Copyright 6WIND S.A., 2014
3a75eb03bSDavid Marchand  *
4a75eb03bSDavid Marchand  * This work is licensed under the terms of the GNU GPL, version 2 or
5a75eb03bSDavid Marchand  * (at your option) any later version.  See the COPYING file in the
6a75eb03bSDavid Marchand  * top-level directory.
7a75eb03bSDavid Marchand  */
8*ccd241b5SPeter Maydell #include "qemu/osdep.h"
9a75eb03bSDavid Marchand #include "qemu-common.h"
10a75eb03bSDavid Marchand #include "qemu/sockets.h"
11a75eb03bSDavid Marchand 
12a75eb03bSDavid Marchand #include <sys/mman.h>
13a75eb03bSDavid Marchand #include <sys/socket.h>
14a75eb03bSDavid Marchand #include <sys/un.h>
151e21feb6SMarc-André Lureau #ifdef CONFIG_LINUX
161e21feb6SMarc-André Lureau #include <sys/vfs.h>
171e21feb6SMarc-André Lureau #endif
18a75eb03bSDavid Marchand 
19a75eb03bSDavid Marchand #include "ivshmem-server.h"
20a75eb03bSDavid Marchand 
21a75eb03bSDavid Marchand /* log a message on stdout if verbose=1 */
22a75eb03bSDavid Marchand #define IVSHMEM_SERVER_DEBUG(server, fmt, ...) do { \
23a75eb03bSDavid Marchand         if ((server)->verbose) {         \
24a75eb03bSDavid Marchand             printf(fmt, ## __VA_ARGS__); \
25a75eb03bSDavid Marchand         }                                \
26a75eb03bSDavid Marchand     } while (0)
27a75eb03bSDavid Marchand 
28a75eb03bSDavid Marchand /** maximum size of a huge page, used by ivshmem_server_ftruncate() */
29a75eb03bSDavid Marchand #define IVSHMEM_SERVER_MAX_HUGEPAGE_SIZE (1024 * 1024 * 1024)
30a75eb03bSDavid Marchand 
31a75eb03bSDavid Marchand /** default listen backlog (number of sockets not accepted) */
32a75eb03bSDavid Marchand #define IVSHMEM_SERVER_LISTEN_BACKLOG 10
33a75eb03bSDavid Marchand 
34a75eb03bSDavid Marchand /* send message to a client unix socket */
35a75eb03bSDavid Marchand static int
36f7a199b2SMarc-André Lureau ivshmem_server_send_one_msg(int sock_fd, int64_t peer_id, int fd)
37a75eb03bSDavid Marchand {
38a75eb03bSDavid Marchand     int ret;
39a75eb03bSDavid Marchand     struct msghdr msg;
40a75eb03bSDavid Marchand     struct iovec iov[1];
41a75eb03bSDavid Marchand     union {
42a75eb03bSDavid Marchand         struct cmsghdr cmsg;
43a75eb03bSDavid Marchand         char control[CMSG_SPACE(sizeof(int))];
44a75eb03bSDavid Marchand     } msg_control;
45a75eb03bSDavid Marchand     struct cmsghdr *cmsg;
46a75eb03bSDavid Marchand 
47f7a199b2SMarc-André Lureau     peer_id = GINT64_TO_LE(peer_id);
48a75eb03bSDavid Marchand     iov[0].iov_base = &peer_id;
49a75eb03bSDavid Marchand     iov[0].iov_len = sizeof(peer_id);
50a75eb03bSDavid Marchand 
51a75eb03bSDavid Marchand     memset(&msg, 0, sizeof(msg));
52a75eb03bSDavid Marchand     msg.msg_iov = iov;
53a75eb03bSDavid Marchand     msg.msg_iovlen = 1;
54a75eb03bSDavid Marchand 
55a75eb03bSDavid Marchand     /* if fd is specified, add it in a cmsg */
56a75eb03bSDavid Marchand     if (fd >= 0) {
57a75eb03bSDavid Marchand         memset(&msg_control, 0, sizeof(msg_control));
58a75eb03bSDavid Marchand         msg.msg_control = &msg_control;
59a75eb03bSDavid Marchand         msg.msg_controllen = sizeof(msg_control);
60a75eb03bSDavid Marchand         cmsg = CMSG_FIRSTHDR(&msg);
61a75eb03bSDavid Marchand         cmsg->cmsg_level = SOL_SOCKET;
62a75eb03bSDavid Marchand         cmsg->cmsg_type = SCM_RIGHTS;
63a75eb03bSDavid Marchand         cmsg->cmsg_len = CMSG_LEN(sizeof(int));
64a75eb03bSDavid Marchand         memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
65a75eb03bSDavid Marchand     }
66a75eb03bSDavid Marchand 
67a75eb03bSDavid Marchand     ret = sendmsg(sock_fd, &msg, 0);
68a75eb03bSDavid Marchand     if (ret <= 0) {
69a75eb03bSDavid Marchand         return -1;
70a75eb03bSDavid Marchand     }
71a75eb03bSDavid Marchand 
72a75eb03bSDavid Marchand     return 0;
73a75eb03bSDavid Marchand }
74a75eb03bSDavid Marchand 
75a75eb03bSDavid Marchand /* free a peer when the server advertises a disconnection or when the
76a75eb03bSDavid Marchand  * server is freed */
77a75eb03bSDavid Marchand static void
78a75eb03bSDavid Marchand ivshmem_server_free_peer(IvshmemServer *server, IvshmemServerPeer *peer)
79a75eb03bSDavid Marchand {
80a75eb03bSDavid Marchand     unsigned vector;
81a75eb03bSDavid Marchand     IvshmemServerPeer *other_peer;
82a75eb03bSDavid Marchand 
83f7a199b2SMarc-André Lureau     IVSHMEM_SERVER_DEBUG(server, "free peer %" PRId64 "\n", peer->id);
84a75eb03bSDavid Marchand     close(peer->sock_fd);
85a75eb03bSDavid Marchand     QTAILQ_REMOVE(&server->peer_list, peer, next);
86a75eb03bSDavid Marchand 
87a75eb03bSDavid Marchand     /* advertise the deletion to other peers */
88a75eb03bSDavid Marchand     QTAILQ_FOREACH(other_peer, &server->peer_list, next) {
89a75eb03bSDavid Marchand         ivshmem_server_send_one_msg(other_peer->sock_fd, peer->id, -1);
90a75eb03bSDavid Marchand     }
91a75eb03bSDavid Marchand 
92a75eb03bSDavid Marchand     for (vector = 0; vector < peer->vectors_count; vector++) {
93a75eb03bSDavid Marchand         event_notifier_cleanup(&peer->vectors[vector]);
94a75eb03bSDavid Marchand     }
95a75eb03bSDavid Marchand 
96a75eb03bSDavid Marchand     g_free(peer);
97a75eb03bSDavid Marchand }
98a75eb03bSDavid Marchand 
99a75eb03bSDavid Marchand /* send the peer id and the shm_fd just after a new client connection */
100a75eb03bSDavid Marchand static int
101a75eb03bSDavid Marchand ivshmem_server_send_initial_info(IvshmemServer *server, IvshmemServerPeer *peer)
102a75eb03bSDavid Marchand {
103a75eb03bSDavid Marchand     int ret;
104a75eb03bSDavid Marchand 
1055105b1d8SDavid Marchand     /* send our protocol version first */
1065105b1d8SDavid Marchand     ret = ivshmem_server_send_one_msg(peer->sock_fd, IVSHMEM_PROTOCOL_VERSION,
1075105b1d8SDavid Marchand                                       -1);
1085105b1d8SDavid Marchand     if (ret < 0) {
1095105b1d8SDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "cannot send version: %s\n",
1105105b1d8SDavid Marchand                              strerror(errno));
1115105b1d8SDavid Marchand         return -1;
1125105b1d8SDavid Marchand     }
1135105b1d8SDavid Marchand 
114a75eb03bSDavid Marchand     /* send the peer id to the client */
115a75eb03bSDavid Marchand     ret = ivshmem_server_send_one_msg(peer->sock_fd, peer->id, -1);
116a75eb03bSDavid Marchand     if (ret < 0) {
117a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "cannot send peer id: %s\n",
118a75eb03bSDavid Marchand                              strerror(errno));
119a75eb03bSDavid Marchand         return -1;
120a75eb03bSDavid Marchand     }
121a75eb03bSDavid Marchand 
122a75eb03bSDavid Marchand     /* send the shm_fd */
123a75eb03bSDavid Marchand     ret = ivshmem_server_send_one_msg(peer->sock_fd, -1, server->shm_fd);
124a75eb03bSDavid Marchand     if (ret < 0) {
125a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "cannot send shm fd: %s\n",
126a75eb03bSDavid Marchand                              strerror(errno));
127a75eb03bSDavid Marchand         return -1;
128a75eb03bSDavid Marchand     }
129a75eb03bSDavid Marchand 
130a75eb03bSDavid Marchand     return 0;
131a75eb03bSDavid Marchand }
132a75eb03bSDavid Marchand 
133a75eb03bSDavid Marchand /* handle message on listening unix socket (new client connection) */
134a75eb03bSDavid Marchand static int
135a75eb03bSDavid Marchand ivshmem_server_handle_new_conn(IvshmemServer *server)
136a75eb03bSDavid Marchand {
137a75eb03bSDavid Marchand     IvshmemServerPeer *peer, *other_peer;
138a75eb03bSDavid Marchand     struct sockaddr_un unaddr;
139a75eb03bSDavid Marchand     socklen_t unaddr_len;
140a75eb03bSDavid Marchand     int newfd;
141a75eb03bSDavid Marchand     unsigned i;
142a75eb03bSDavid Marchand 
143a75eb03bSDavid Marchand     /* accept the incoming connection */
144a75eb03bSDavid Marchand     unaddr_len = sizeof(unaddr);
145a75eb03bSDavid Marchand     newfd = qemu_accept(server->sock_fd,
146a75eb03bSDavid Marchand                         (struct sockaddr *)&unaddr, &unaddr_len);
147a75eb03bSDavid Marchand 
148a75eb03bSDavid Marchand     if (newfd < 0) {
149a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "cannot accept() %s\n", strerror(errno));
150a75eb03bSDavid Marchand         return -1;
151a75eb03bSDavid Marchand     }
152a75eb03bSDavid Marchand 
153a75eb03bSDavid Marchand     qemu_set_nonblock(newfd);
154a75eb03bSDavid Marchand     IVSHMEM_SERVER_DEBUG(server, "accept()=%d\n", newfd);
155a75eb03bSDavid Marchand 
156a75eb03bSDavid Marchand     /* allocate new structure for this peer */
157a75eb03bSDavid Marchand     peer = g_malloc0(sizeof(*peer));
158a75eb03bSDavid Marchand     peer->sock_fd = newfd;
159a75eb03bSDavid Marchand 
160a75eb03bSDavid Marchand     /* get an unused peer id */
161022cffe3SMarc-André Lureau     /* XXX: this could use id allocation such as Linux IDA, or simply
162022cffe3SMarc-André Lureau      * a free-list */
163022cffe3SMarc-André Lureau     for (i = 0; i < G_MAXUINT16; i++) {
164022cffe3SMarc-André Lureau         if (ivshmem_server_search_peer(server, server->cur_id) == NULL) {
165022cffe3SMarc-André Lureau             break;
166022cffe3SMarc-André Lureau         }
167a75eb03bSDavid Marchand         server->cur_id++;
168a75eb03bSDavid Marchand     }
169022cffe3SMarc-André Lureau     if (i == G_MAXUINT16) {
170022cffe3SMarc-André Lureau         IVSHMEM_SERVER_DEBUG(server, "cannot allocate new client id\n");
171258133bdSGonglei         close(newfd);
172258133bdSGonglei         g_free(peer);
173258133bdSGonglei         return -1;
174022cffe3SMarc-André Lureau     }
175a75eb03bSDavid Marchand     peer->id = server->cur_id++;
176a75eb03bSDavid Marchand 
177a75eb03bSDavid Marchand     /* create eventfd, one per vector */
178a75eb03bSDavid Marchand     peer->vectors_count = server->n_vectors;
179a75eb03bSDavid Marchand     for (i = 0; i < peer->vectors_count; i++) {
180a75eb03bSDavid Marchand         if (event_notifier_init(&peer->vectors[i], FALSE) < 0) {
181a75eb03bSDavid Marchand             IVSHMEM_SERVER_DEBUG(server, "cannot create eventfd\n");
182a75eb03bSDavid Marchand             goto fail;
183a75eb03bSDavid Marchand         }
184a75eb03bSDavid Marchand     }
185a75eb03bSDavid Marchand 
186a75eb03bSDavid Marchand     /* send peer id and shm fd */
187a75eb03bSDavid Marchand     if (ivshmem_server_send_initial_info(server, peer) < 0) {
188a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "cannot send initial info\n");
189a75eb03bSDavid Marchand         goto fail;
190a75eb03bSDavid Marchand     }
191a75eb03bSDavid Marchand 
192a75eb03bSDavid Marchand     /* advertise the new peer to others */
193a75eb03bSDavid Marchand     QTAILQ_FOREACH(other_peer, &server->peer_list, next) {
194a75eb03bSDavid Marchand         for (i = 0; i < peer->vectors_count; i++) {
195a75eb03bSDavid Marchand             ivshmem_server_send_one_msg(other_peer->sock_fd, peer->id,
196a75eb03bSDavid Marchand                                         peer->vectors[i].wfd);
197a75eb03bSDavid Marchand         }
198a75eb03bSDavid Marchand     }
199a75eb03bSDavid Marchand 
200a75eb03bSDavid Marchand     /* advertise the other peers to the new one */
201a75eb03bSDavid Marchand     QTAILQ_FOREACH(other_peer, &server->peer_list, next) {
202a75eb03bSDavid Marchand         for (i = 0; i < peer->vectors_count; i++) {
203a75eb03bSDavid Marchand             ivshmem_server_send_one_msg(peer->sock_fd, other_peer->id,
204a75eb03bSDavid Marchand                                         other_peer->vectors[i].wfd);
205a75eb03bSDavid Marchand         }
206a75eb03bSDavid Marchand     }
207a75eb03bSDavid Marchand 
208a75eb03bSDavid Marchand     /* advertise the new peer to itself */
209a75eb03bSDavid Marchand     for (i = 0; i < peer->vectors_count; i++) {
210a75eb03bSDavid Marchand         ivshmem_server_send_one_msg(peer->sock_fd, peer->id,
211a75eb03bSDavid Marchand                                     event_notifier_get_fd(&peer->vectors[i]));
212a75eb03bSDavid Marchand     }
213a75eb03bSDavid Marchand 
214a75eb03bSDavid Marchand     QTAILQ_INSERT_TAIL(&server->peer_list, peer, next);
215f7a199b2SMarc-André Lureau     IVSHMEM_SERVER_DEBUG(server, "new peer id = %" PRId64 "\n",
216a75eb03bSDavid Marchand                          peer->id);
217a75eb03bSDavid Marchand     return 0;
218a75eb03bSDavid Marchand 
219a75eb03bSDavid Marchand fail:
220a75eb03bSDavid Marchand     while (i--) {
221a75eb03bSDavid Marchand         event_notifier_cleanup(&peer->vectors[i]);
222a75eb03bSDavid Marchand     }
223a75eb03bSDavid Marchand     close(newfd);
224a75eb03bSDavid Marchand     g_free(peer);
225a75eb03bSDavid Marchand     return -1;
226a75eb03bSDavid Marchand }
227a75eb03bSDavid Marchand 
228a75eb03bSDavid Marchand /* Try to ftruncate a file to next power of 2 of shmsize.
229a75eb03bSDavid Marchand  * If it fails; all power of 2 above shmsize are tested until
230a75eb03bSDavid Marchand  * we reach the maximum huge page size. This is useful
231a75eb03bSDavid Marchand  * if the shm file is in a hugetlbfs that cannot be truncated to the
232a75eb03bSDavid Marchand  * shm_size value. */
233a75eb03bSDavid Marchand static int
234a75eb03bSDavid Marchand ivshmem_server_ftruncate(int fd, unsigned shmsize)
235a75eb03bSDavid Marchand {
236a75eb03bSDavid Marchand     int ret;
237a75eb03bSDavid Marchand     struct stat mapstat;
238a75eb03bSDavid Marchand 
239a75eb03bSDavid Marchand     /* align shmsize to next power of 2 */
240a75eb03bSDavid Marchand     shmsize = pow2ceil(shmsize);
241a75eb03bSDavid Marchand 
242a75eb03bSDavid Marchand     if (fstat(fd, &mapstat) != -1 && mapstat.st_size == shmsize) {
243a75eb03bSDavid Marchand         return 0;
244a75eb03bSDavid Marchand     }
245a75eb03bSDavid Marchand 
246a75eb03bSDavid Marchand     while (shmsize <= IVSHMEM_SERVER_MAX_HUGEPAGE_SIZE) {
247a75eb03bSDavid Marchand         ret = ftruncate(fd, shmsize);
248a75eb03bSDavid Marchand         if (ret == 0) {
249a75eb03bSDavid Marchand             return ret;
250a75eb03bSDavid Marchand         }
251a75eb03bSDavid Marchand         shmsize *= 2;
252a75eb03bSDavid Marchand     }
253a75eb03bSDavid Marchand 
254a75eb03bSDavid Marchand     return -1;
255a75eb03bSDavid Marchand }
256a75eb03bSDavid Marchand 
257a75eb03bSDavid Marchand /* Init a new ivshmem server */
258a75eb03bSDavid Marchand int
259a75eb03bSDavid Marchand ivshmem_server_init(IvshmemServer *server, const char *unix_sock_path,
260a75eb03bSDavid Marchand                     const char *shm_path, size_t shm_size, unsigned n_vectors,
261a75eb03bSDavid Marchand                     bool verbose)
262a75eb03bSDavid Marchand {
263a75eb03bSDavid Marchand     int ret;
264a75eb03bSDavid Marchand 
265a75eb03bSDavid Marchand     memset(server, 0, sizeof(*server));
266a75eb03bSDavid Marchand     server->verbose = verbose;
267a75eb03bSDavid Marchand 
268a75eb03bSDavid Marchand     ret = snprintf(server->unix_sock_path, sizeof(server->unix_sock_path),
269a75eb03bSDavid Marchand                    "%s", unix_sock_path);
270a75eb03bSDavid Marchand     if (ret < 0 || ret >= sizeof(server->unix_sock_path)) {
271a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "could not copy unix socket path\n");
272a75eb03bSDavid Marchand         return -1;
273a75eb03bSDavid Marchand     }
274a75eb03bSDavid Marchand     ret = snprintf(server->shm_path, sizeof(server->shm_path),
275a75eb03bSDavid Marchand                    "%s", shm_path);
276a75eb03bSDavid Marchand     if (ret < 0 || ret >= sizeof(server->shm_path)) {
277a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "could not copy shm path\n");
278a75eb03bSDavid Marchand         return -1;
279a75eb03bSDavid Marchand     }
280a75eb03bSDavid Marchand 
281a75eb03bSDavid Marchand     server->shm_size = shm_size;
282a75eb03bSDavid Marchand     server->n_vectors = n_vectors;
283a75eb03bSDavid Marchand 
284a75eb03bSDavid Marchand     QTAILQ_INIT(&server->peer_list);
285a75eb03bSDavid Marchand 
286a75eb03bSDavid Marchand     return 0;
287a75eb03bSDavid Marchand }
288a75eb03bSDavid Marchand 
2891e21feb6SMarc-André Lureau #ifdef CONFIG_LINUX
2901e21feb6SMarc-André Lureau 
2911e21feb6SMarc-André Lureau #define HUGETLBFS_MAGIC       0x958458f6
2921e21feb6SMarc-André Lureau 
2931e21feb6SMarc-André Lureau static long gethugepagesize(const char *path)
2941e21feb6SMarc-André Lureau {
2951e21feb6SMarc-André Lureau     struct statfs fs;
2961e21feb6SMarc-André Lureau     int ret;
2971e21feb6SMarc-André Lureau 
2981e21feb6SMarc-André Lureau     do {
2991e21feb6SMarc-André Lureau         ret = statfs(path, &fs);
3001e21feb6SMarc-André Lureau     } while (ret != 0 && errno == EINTR);
3011e21feb6SMarc-André Lureau 
3021e21feb6SMarc-André Lureau     if (ret != 0) {
3031e21feb6SMarc-André Lureau         return -1;
3041e21feb6SMarc-André Lureau     }
3051e21feb6SMarc-André Lureau 
3061e21feb6SMarc-André Lureau     if (fs.f_type != HUGETLBFS_MAGIC) {
3071e21feb6SMarc-André Lureau         return -1;
3081e21feb6SMarc-André Lureau     }
3091e21feb6SMarc-André Lureau 
3101e21feb6SMarc-André Lureau     return fs.f_bsize;
3111e21feb6SMarc-André Lureau }
3121e21feb6SMarc-André Lureau #endif
3131e21feb6SMarc-André Lureau 
314a75eb03bSDavid Marchand /* open shm, create and bind to the unix socket */
315a75eb03bSDavid Marchand int
316a75eb03bSDavid Marchand ivshmem_server_start(IvshmemServer *server)
317a75eb03bSDavid Marchand {
318a75eb03bSDavid Marchand     struct sockaddr_un sun;
319a75eb03bSDavid Marchand     int shm_fd, sock_fd, ret;
320a75eb03bSDavid Marchand 
321a75eb03bSDavid Marchand     /* open shm file */
3221e21feb6SMarc-André Lureau #ifdef CONFIG_LINUX
3231e21feb6SMarc-André Lureau     long hpagesize;
3241e21feb6SMarc-André Lureau 
3251e21feb6SMarc-André Lureau     hpagesize = gethugepagesize(server->shm_path);
3261e21feb6SMarc-André Lureau     if (hpagesize < 0 && errno != ENOENT) {
3271e21feb6SMarc-André Lureau         IVSHMEM_SERVER_DEBUG(server, "cannot stat shm file %s: %s\n",
3281e21feb6SMarc-André Lureau                              server->shm_path, strerror(errno));
3291e21feb6SMarc-André Lureau     }
3301e21feb6SMarc-André Lureau 
3311e21feb6SMarc-André Lureau     if (hpagesize > 0) {
3321e21feb6SMarc-André Lureau         gchar *filename = g_strdup_printf("%s/ivshmem.XXXXXX", server->shm_path);
3331e21feb6SMarc-André Lureau         IVSHMEM_SERVER_DEBUG(server, "Using hugepages: %s\n", server->shm_path);
3341e21feb6SMarc-André Lureau         shm_fd = mkstemp(filename);
3351e21feb6SMarc-André Lureau         unlink(filename);
3361e21feb6SMarc-André Lureau         g_free(filename);
3371e21feb6SMarc-André Lureau     } else
3381e21feb6SMarc-André Lureau #endif
3391e21feb6SMarc-André Lureau     {
3401e21feb6SMarc-André Lureau         IVSHMEM_SERVER_DEBUG(server, "Using POSIX shared memory: %s\n",
3411e21feb6SMarc-André Lureau                              server->shm_path);
342a75eb03bSDavid Marchand         shm_fd = shm_open(server->shm_path, O_CREAT|O_RDWR, S_IRWXU);
3431e21feb6SMarc-André Lureau     }
3441e21feb6SMarc-André Lureau 
345a75eb03bSDavid Marchand     if (shm_fd < 0) {
346a75eb03bSDavid Marchand         fprintf(stderr, "cannot open shm file %s: %s\n", server->shm_path,
347a75eb03bSDavid Marchand                 strerror(errno));
348a75eb03bSDavid Marchand         return -1;
349a75eb03bSDavid Marchand     }
350a75eb03bSDavid Marchand     if (ivshmem_server_ftruncate(shm_fd, server->shm_size) < 0) {
351a75eb03bSDavid Marchand         fprintf(stderr, "ftruncate(%s) failed: %s\n", server->shm_path,
352a75eb03bSDavid Marchand                 strerror(errno));
353a75eb03bSDavid Marchand         goto err_close_shm;
354a75eb03bSDavid Marchand     }
355a75eb03bSDavid Marchand 
356a75eb03bSDavid Marchand     IVSHMEM_SERVER_DEBUG(server, "create & bind socket %s\n",
357a75eb03bSDavid Marchand                          server->unix_sock_path);
358a75eb03bSDavid Marchand 
359a75eb03bSDavid Marchand     /* create the unix listening socket */
360a75eb03bSDavid Marchand     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
361a75eb03bSDavid Marchand     if (sock_fd < 0) {
362a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "cannot create socket: %s\n",
363a75eb03bSDavid Marchand                              strerror(errno));
364a75eb03bSDavid Marchand         goto err_close_shm;
365a75eb03bSDavid Marchand     }
366a75eb03bSDavid Marchand 
367a75eb03bSDavid Marchand     sun.sun_family = AF_UNIX;
368a75eb03bSDavid Marchand     ret = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s",
369a75eb03bSDavid Marchand                    server->unix_sock_path);
370a75eb03bSDavid Marchand     if (ret < 0 || ret >= sizeof(sun.sun_path)) {
371a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "could not copy unix socket path\n");
372a75eb03bSDavid Marchand         goto err_close_sock;
373a75eb03bSDavid Marchand     }
374a75eb03bSDavid Marchand     if (bind(sock_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
375a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "cannot connect to %s: %s\n", sun.sun_path,
376a75eb03bSDavid Marchand                              strerror(errno));
377a75eb03bSDavid Marchand         goto err_close_sock;
378a75eb03bSDavid Marchand     }
379a75eb03bSDavid Marchand 
380a75eb03bSDavid Marchand     if (listen(sock_fd, IVSHMEM_SERVER_LISTEN_BACKLOG) < 0) {
381a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "listen() failed: %s\n", strerror(errno));
382a75eb03bSDavid Marchand         goto err_close_sock;
383a75eb03bSDavid Marchand     }
384a75eb03bSDavid Marchand 
385a75eb03bSDavid Marchand     server->sock_fd = sock_fd;
386a75eb03bSDavid Marchand     server->shm_fd = shm_fd;
387a75eb03bSDavid Marchand 
388a75eb03bSDavid Marchand     return 0;
389a75eb03bSDavid Marchand 
390a75eb03bSDavid Marchand err_close_sock:
391a75eb03bSDavid Marchand     close(sock_fd);
392a75eb03bSDavid Marchand err_close_shm:
393a75eb03bSDavid Marchand     close(shm_fd);
394a75eb03bSDavid Marchand     return -1;
395a75eb03bSDavid Marchand }
396a75eb03bSDavid Marchand 
397a75eb03bSDavid Marchand /* close connections to clients, the unix socket and the shm fd */
398a75eb03bSDavid Marchand void
399a75eb03bSDavid Marchand ivshmem_server_close(IvshmemServer *server)
400a75eb03bSDavid Marchand {
401a75eb03bSDavid Marchand     IvshmemServerPeer *peer, *npeer;
402a75eb03bSDavid Marchand 
403a75eb03bSDavid Marchand     IVSHMEM_SERVER_DEBUG(server, "close server\n");
404a75eb03bSDavid Marchand 
405a75eb03bSDavid Marchand     QTAILQ_FOREACH_SAFE(peer, &server->peer_list, next, npeer) {
406a75eb03bSDavid Marchand         ivshmem_server_free_peer(server, peer);
407a75eb03bSDavid Marchand     }
408a75eb03bSDavid Marchand 
409a75eb03bSDavid Marchand     unlink(server->unix_sock_path);
410a75eb03bSDavid Marchand     close(server->sock_fd);
411a75eb03bSDavid Marchand     close(server->shm_fd);
412a75eb03bSDavid Marchand     server->sock_fd = -1;
413a75eb03bSDavid Marchand     server->shm_fd = -1;
414a75eb03bSDavid Marchand }
415a75eb03bSDavid Marchand 
416a75eb03bSDavid Marchand /* get the fd_set according to the unix socket and the peer list */
417a75eb03bSDavid Marchand void
418a75eb03bSDavid Marchand ivshmem_server_get_fds(const IvshmemServer *server, fd_set *fds, int *maxfd)
419a75eb03bSDavid Marchand {
420a75eb03bSDavid Marchand     IvshmemServerPeer *peer;
421a75eb03bSDavid Marchand 
422a75eb03bSDavid Marchand     if (server->sock_fd == -1) {
423a75eb03bSDavid Marchand         return;
424a75eb03bSDavid Marchand     }
425a75eb03bSDavid Marchand 
426a75eb03bSDavid Marchand     FD_SET(server->sock_fd, fds);
427a75eb03bSDavid Marchand     if (server->sock_fd >= *maxfd) {
428a75eb03bSDavid Marchand         *maxfd = server->sock_fd + 1;
429a75eb03bSDavid Marchand     }
430a75eb03bSDavid Marchand 
431a75eb03bSDavid Marchand     QTAILQ_FOREACH(peer, &server->peer_list, next) {
432a75eb03bSDavid Marchand         FD_SET(peer->sock_fd, fds);
433a75eb03bSDavid Marchand         if (peer->sock_fd >= *maxfd) {
434a75eb03bSDavid Marchand             *maxfd = peer->sock_fd + 1;
435a75eb03bSDavid Marchand         }
436a75eb03bSDavid Marchand     }
437a75eb03bSDavid Marchand }
438a75eb03bSDavid Marchand 
439a75eb03bSDavid Marchand /* process incoming messages on the sockets in fd_set */
440a75eb03bSDavid Marchand int
441a75eb03bSDavid Marchand ivshmem_server_handle_fds(IvshmemServer *server, fd_set *fds, int maxfd)
442a75eb03bSDavid Marchand {
443a75eb03bSDavid Marchand     IvshmemServerPeer *peer, *peer_next;
444a75eb03bSDavid Marchand 
445a75eb03bSDavid Marchand     if (server->sock_fd < maxfd && FD_ISSET(server->sock_fd, fds) &&
446a75eb03bSDavid Marchand         ivshmem_server_handle_new_conn(server) < 0 && errno != EINTR) {
447a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "ivshmem_server_handle_new_conn() "
448a75eb03bSDavid Marchand                              "failed\n");
449a75eb03bSDavid Marchand         return -1;
450a75eb03bSDavid Marchand     }
451a75eb03bSDavid Marchand 
452a75eb03bSDavid Marchand     QTAILQ_FOREACH_SAFE(peer, &server->peer_list, next, peer_next) {
453a75eb03bSDavid Marchand         /* any message from a peer socket result in a close() */
454a75eb03bSDavid Marchand         IVSHMEM_SERVER_DEBUG(server, "peer->sock_fd=%d\n", peer->sock_fd);
455a75eb03bSDavid Marchand         if (peer->sock_fd < maxfd && FD_ISSET(peer->sock_fd, fds)) {
456a75eb03bSDavid Marchand             ivshmem_server_free_peer(server, peer);
457a75eb03bSDavid Marchand         }
458a75eb03bSDavid Marchand     }
459a75eb03bSDavid Marchand 
460a75eb03bSDavid Marchand     return 0;
461a75eb03bSDavid Marchand }
462a75eb03bSDavid Marchand 
463a75eb03bSDavid Marchand /* lookup peer from its id */
464a75eb03bSDavid Marchand IvshmemServerPeer *
465f7a199b2SMarc-André Lureau ivshmem_server_search_peer(IvshmemServer *server, int64_t peer_id)
466a75eb03bSDavid Marchand {
467a75eb03bSDavid Marchand     IvshmemServerPeer *peer;
468a75eb03bSDavid Marchand 
469a75eb03bSDavid Marchand     QTAILQ_FOREACH(peer, &server->peer_list, next) {
470a75eb03bSDavid Marchand         if (peer->id == peer_id) {
471a75eb03bSDavid Marchand             return peer;
472a75eb03bSDavid Marchand         }
473a75eb03bSDavid Marchand     }
474a75eb03bSDavid Marchand     return NULL;
475a75eb03bSDavid Marchand }
476a75eb03bSDavid Marchand 
477a75eb03bSDavid Marchand /* dump our info, the list of peers their vectors on stdout */
478a75eb03bSDavid Marchand void
479a75eb03bSDavid Marchand ivshmem_server_dump(const IvshmemServer *server)
480a75eb03bSDavid Marchand {
481a75eb03bSDavid Marchand     const IvshmemServerPeer *peer;
482a75eb03bSDavid Marchand     unsigned vector;
483a75eb03bSDavid Marchand 
484a75eb03bSDavid Marchand     /* dump peers */
485a75eb03bSDavid Marchand     QTAILQ_FOREACH(peer, &server->peer_list, next) {
486f7a199b2SMarc-André Lureau         printf("peer_id = %" PRId64 "\n", peer->id);
487a75eb03bSDavid Marchand 
488a75eb03bSDavid Marchand         for (vector = 0; vector < peer->vectors_count; vector++) {
489a75eb03bSDavid Marchand             printf("  vector %d is enabled (fd=%d)\n", vector,
490a75eb03bSDavid Marchand                    event_notifier_get_fd(&peer->vectors[vector]));
491a75eb03bSDavid Marchand         }
492a75eb03bSDavid Marchand     }
493a75eb03bSDavid Marchand }
494