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 */ 8a75eb03bSDavid Marchand #include "qemu-common.h" 9a75eb03bSDavid Marchand #include "qemu/sockets.h" 10a75eb03bSDavid Marchand 11a75eb03bSDavid Marchand #include <sys/mman.h> 12a75eb03bSDavid Marchand #include <sys/types.h> 13a75eb03bSDavid Marchand #include <sys/socket.h> 14a75eb03bSDavid Marchand #include <sys/un.h> 15*1e21feb6SMarc-André Lureau #ifdef CONFIG_LINUX 16*1e21feb6SMarc-André Lureau #include <sys/vfs.h> 17*1e21feb6SMarc-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 36a75eb03bSDavid Marchand ivshmem_server_send_one_msg(int sock_fd, long 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 47a75eb03bSDavid Marchand iov[0].iov_base = &peer_id; 48a75eb03bSDavid Marchand iov[0].iov_len = sizeof(peer_id); 49a75eb03bSDavid Marchand 50a75eb03bSDavid Marchand memset(&msg, 0, sizeof(msg)); 51a75eb03bSDavid Marchand msg.msg_iov = iov; 52a75eb03bSDavid Marchand msg.msg_iovlen = 1; 53a75eb03bSDavid Marchand 54a75eb03bSDavid Marchand /* if fd is specified, add it in a cmsg */ 55a75eb03bSDavid Marchand if (fd >= 0) { 56a75eb03bSDavid Marchand memset(&msg_control, 0, sizeof(msg_control)); 57a75eb03bSDavid Marchand msg.msg_control = &msg_control; 58a75eb03bSDavid Marchand msg.msg_controllen = sizeof(msg_control); 59a75eb03bSDavid Marchand cmsg = CMSG_FIRSTHDR(&msg); 60a75eb03bSDavid Marchand cmsg->cmsg_level = SOL_SOCKET; 61a75eb03bSDavid Marchand cmsg->cmsg_type = SCM_RIGHTS; 62a75eb03bSDavid Marchand cmsg->cmsg_len = CMSG_LEN(sizeof(int)); 63a75eb03bSDavid Marchand memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); 64a75eb03bSDavid Marchand } 65a75eb03bSDavid Marchand 66a75eb03bSDavid Marchand ret = sendmsg(sock_fd, &msg, 0); 67a75eb03bSDavid Marchand if (ret <= 0) { 68a75eb03bSDavid Marchand return -1; 69a75eb03bSDavid Marchand } 70a75eb03bSDavid Marchand 71a75eb03bSDavid Marchand return 0; 72a75eb03bSDavid Marchand } 73a75eb03bSDavid Marchand 74a75eb03bSDavid Marchand /* free a peer when the server advertises a disconnection or when the 75a75eb03bSDavid Marchand * server is freed */ 76a75eb03bSDavid Marchand static void 77a75eb03bSDavid Marchand ivshmem_server_free_peer(IvshmemServer *server, IvshmemServerPeer *peer) 78a75eb03bSDavid Marchand { 79a75eb03bSDavid Marchand unsigned vector; 80a75eb03bSDavid Marchand IvshmemServerPeer *other_peer; 81a75eb03bSDavid Marchand 82a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "free peer %ld\n", peer->id); 83a75eb03bSDavid Marchand close(peer->sock_fd); 84a75eb03bSDavid Marchand QTAILQ_REMOVE(&server->peer_list, peer, next); 85a75eb03bSDavid Marchand 86a75eb03bSDavid Marchand /* advertise the deletion to other peers */ 87a75eb03bSDavid Marchand QTAILQ_FOREACH(other_peer, &server->peer_list, next) { 88a75eb03bSDavid Marchand ivshmem_server_send_one_msg(other_peer->sock_fd, peer->id, -1); 89a75eb03bSDavid Marchand } 90a75eb03bSDavid Marchand 91a75eb03bSDavid Marchand for (vector = 0; vector < peer->vectors_count; vector++) { 92a75eb03bSDavid Marchand event_notifier_cleanup(&peer->vectors[vector]); 93a75eb03bSDavid Marchand } 94a75eb03bSDavid Marchand 95a75eb03bSDavid Marchand g_free(peer); 96a75eb03bSDavid Marchand } 97a75eb03bSDavid Marchand 98a75eb03bSDavid Marchand /* send the peer id and the shm_fd just after a new client connection */ 99a75eb03bSDavid Marchand static int 100a75eb03bSDavid Marchand ivshmem_server_send_initial_info(IvshmemServer *server, IvshmemServerPeer *peer) 101a75eb03bSDavid Marchand { 102a75eb03bSDavid Marchand int ret; 103a75eb03bSDavid Marchand 104a75eb03bSDavid Marchand /* send the peer id to the client */ 105a75eb03bSDavid Marchand ret = ivshmem_server_send_one_msg(peer->sock_fd, peer->id, -1); 106a75eb03bSDavid Marchand if (ret < 0) { 107a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send peer id: %s\n", 108a75eb03bSDavid Marchand strerror(errno)); 109a75eb03bSDavid Marchand return -1; 110a75eb03bSDavid Marchand } 111a75eb03bSDavid Marchand 112a75eb03bSDavid Marchand /* send the shm_fd */ 113a75eb03bSDavid Marchand ret = ivshmem_server_send_one_msg(peer->sock_fd, -1, server->shm_fd); 114a75eb03bSDavid Marchand if (ret < 0) { 115a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send shm fd: %s\n", 116a75eb03bSDavid Marchand strerror(errno)); 117a75eb03bSDavid Marchand return -1; 118a75eb03bSDavid Marchand } 119a75eb03bSDavid Marchand 120a75eb03bSDavid Marchand return 0; 121a75eb03bSDavid Marchand } 122a75eb03bSDavid Marchand 123a75eb03bSDavid Marchand /* handle message on listening unix socket (new client connection) */ 124a75eb03bSDavid Marchand static int 125a75eb03bSDavid Marchand ivshmem_server_handle_new_conn(IvshmemServer *server) 126a75eb03bSDavid Marchand { 127a75eb03bSDavid Marchand IvshmemServerPeer *peer, *other_peer; 128a75eb03bSDavid Marchand struct sockaddr_un unaddr; 129a75eb03bSDavid Marchand socklen_t unaddr_len; 130a75eb03bSDavid Marchand int newfd; 131a75eb03bSDavid Marchand unsigned i; 132a75eb03bSDavid Marchand 133a75eb03bSDavid Marchand /* accept the incoming connection */ 134a75eb03bSDavid Marchand unaddr_len = sizeof(unaddr); 135a75eb03bSDavid Marchand newfd = qemu_accept(server->sock_fd, 136a75eb03bSDavid Marchand (struct sockaddr *)&unaddr, &unaddr_len); 137a75eb03bSDavid Marchand 138a75eb03bSDavid Marchand if (newfd < 0) { 139a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot accept() %s\n", strerror(errno)); 140a75eb03bSDavid Marchand return -1; 141a75eb03bSDavid Marchand } 142a75eb03bSDavid Marchand 143a75eb03bSDavid Marchand qemu_set_nonblock(newfd); 144a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "accept()=%d\n", newfd); 145a75eb03bSDavid Marchand 146a75eb03bSDavid Marchand /* allocate new structure for this peer */ 147a75eb03bSDavid Marchand peer = g_malloc0(sizeof(*peer)); 148a75eb03bSDavid Marchand peer->sock_fd = newfd; 149a75eb03bSDavid Marchand 150a75eb03bSDavid Marchand /* get an unused peer id */ 151022cffe3SMarc-André Lureau /* XXX: this could use id allocation such as Linux IDA, or simply 152022cffe3SMarc-André Lureau * a free-list */ 153022cffe3SMarc-André Lureau for (i = 0; i < G_MAXUINT16; i++) { 154022cffe3SMarc-André Lureau if (ivshmem_server_search_peer(server, server->cur_id) == NULL) { 155022cffe3SMarc-André Lureau break; 156022cffe3SMarc-André Lureau } 157a75eb03bSDavid Marchand server->cur_id++; 158a75eb03bSDavid Marchand } 159022cffe3SMarc-André Lureau if (i == G_MAXUINT16) { 160022cffe3SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "cannot allocate new client id\n"); 161022cffe3SMarc-André Lureau goto fail; 162022cffe3SMarc-André Lureau } 163a75eb03bSDavid Marchand peer->id = server->cur_id++; 164a75eb03bSDavid Marchand 165a75eb03bSDavid Marchand /* create eventfd, one per vector */ 166a75eb03bSDavid Marchand peer->vectors_count = server->n_vectors; 167a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 168a75eb03bSDavid Marchand if (event_notifier_init(&peer->vectors[i], FALSE) < 0) { 169a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot create eventfd\n"); 170a75eb03bSDavid Marchand goto fail; 171a75eb03bSDavid Marchand } 172a75eb03bSDavid Marchand } 173a75eb03bSDavid Marchand 174a75eb03bSDavid Marchand /* send peer id and shm fd */ 175a75eb03bSDavid Marchand if (ivshmem_server_send_initial_info(server, peer) < 0) { 176a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send initial info\n"); 177a75eb03bSDavid Marchand goto fail; 178a75eb03bSDavid Marchand } 179a75eb03bSDavid Marchand 180a75eb03bSDavid Marchand /* advertise the new peer to others */ 181a75eb03bSDavid Marchand QTAILQ_FOREACH(other_peer, &server->peer_list, next) { 182a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 183a75eb03bSDavid Marchand ivshmem_server_send_one_msg(other_peer->sock_fd, peer->id, 184a75eb03bSDavid Marchand peer->vectors[i].wfd); 185a75eb03bSDavid Marchand } 186a75eb03bSDavid Marchand } 187a75eb03bSDavid Marchand 188a75eb03bSDavid Marchand /* advertise the other peers to the new one */ 189a75eb03bSDavid Marchand QTAILQ_FOREACH(other_peer, &server->peer_list, next) { 190a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 191a75eb03bSDavid Marchand ivshmem_server_send_one_msg(peer->sock_fd, other_peer->id, 192a75eb03bSDavid Marchand other_peer->vectors[i].wfd); 193a75eb03bSDavid Marchand } 194a75eb03bSDavid Marchand } 195a75eb03bSDavid Marchand 196a75eb03bSDavid Marchand /* advertise the new peer to itself */ 197a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 198a75eb03bSDavid Marchand ivshmem_server_send_one_msg(peer->sock_fd, peer->id, 199a75eb03bSDavid Marchand event_notifier_get_fd(&peer->vectors[i])); 200a75eb03bSDavid Marchand } 201a75eb03bSDavid Marchand 202a75eb03bSDavid Marchand QTAILQ_INSERT_TAIL(&server->peer_list, peer, next); 203a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "new peer id = %ld\n", 204a75eb03bSDavid Marchand peer->id); 205a75eb03bSDavid Marchand return 0; 206a75eb03bSDavid Marchand 207a75eb03bSDavid Marchand fail: 208a75eb03bSDavid Marchand while (i--) { 209a75eb03bSDavid Marchand event_notifier_cleanup(&peer->vectors[i]); 210a75eb03bSDavid Marchand } 211a75eb03bSDavid Marchand close(newfd); 212a75eb03bSDavid Marchand g_free(peer); 213a75eb03bSDavid Marchand return -1; 214a75eb03bSDavid Marchand } 215a75eb03bSDavid Marchand 216a75eb03bSDavid Marchand /* Try to ftruncate a file to next power of 2 of shmsize. 217a75eb03bSDavid Marchand * If it fails; all power of 2 above shmsize are tested until 218a75eb03bSDavid Marchand * we reach the maximum huge page size. This is useful 219a75eb03bSDavid Marchand * if the shm file is in a hugetlbfs that cannot be truncated to the 220a75eb03bSDavid Marchand * shm_size value. */ 221a75eb03bSDavid Marchand static int 222a75eb03bSDavid Marchand ivshmem_server_ftruncate(int fd, unsigned shmsize) 223a75eb03bSDavid Marchand { 224a75eb03bSDavid Marchand int ret; 225a75eb03bSDavid Marchand struct stat mapstat; 226a75eb03bSDavid Marchand 227a75eb03bSDavid Marchand /* align shmsize to next power of 2 */ 228a75eb03bSDavid Marchand shmsize = pow2ceil(shmsize); 229a75eb03bSDavid Marchand 230a75eb03bSDavid Marchand if (fstat(fd, &mapstat) != -1 && mapstat.st_size == shmsize) { 231a75eb03bSDavid Marchand return 0; 232a75eb03bSDavid Marchand } 233a75eb03bSDavid Marchand 234a75eb03bSDavid Marchand while (shmsize <= IVSHMEM_SERVER_MAX_HUGEPAGE_SIZE) { 235a75eb03bSDavid Marchand ret = ftruncate(fd, shmsize); 236a75eb03bSDavid Marchand if (ret == 0) { 237a75eb03bSDavid Marchand return ret; 238a75eb03bSDavid Marchand } 239a75eb03bSDavid Marchand shmsize *= 2; 240a75eb03bSDavid Marchand } 241a75eb03bSDavid Marchand 242a75eb03bSDavid Marchand return -1; 243a75eb03bSDavid Marchand } 244a75eb03bSDavid Marchand 245a75eb03bSDavid Marchand /* Init a new ivshmem server */ 246a75eb03bSDavid Marchand int 247a75eb03bSDavid Marchand ivshmem_server_init(IvshmemServer *server, const char *unix_sock_path, 248a75eb03bSDavid Marchand const char *shm_path, size_t shm_size, unsigned n_vectors, 249a75eb03bSDavid Marchand bool verbose) 250a75eb03bSDavid Marchand { 251a75eb03bSDavid Marchand int ret; 252a75eb03bSDavid Marchand 253a75eb03bSDavid Marchand memset(server, 0, sizeof(*server)); 254a75eb03bSDavid Marchand server->verbose = verbose; 255a75eb03bSDavid Marchand 256a75eb03bSDavid Marchand ret = snprintf(server->unix_sock_path, sizeof(server->unix_sock_path), 257a75eb03bSDavid Marchand "%s", unix_sock_path); 258a75eb03bSDavid Marchand if (ret < 0 || ret >= sizeof(server->unix_sock_path)) { 259a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "could not copy unix socket path\n"); 260a75eb03bSDavid Marchand return -1; 261a75eb03bSDavid Marchand } 262a75eb03bSDavid Marchand ret = snprintf(server->shm_path, sizeof(server->shm_path), 263a75eb03bSDavid Marchand "%s", shm_path); 264a75eb03bSDavid Marchand if (ret < 0 || ret >= sizeof(server->shm_path)) { 265a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "could not copy shm path\n"); 266a75eb03bSDavid Marchand return -1; 267a75eb03bSDavid Marchand } 268a75eb03bSDavid Marchand 269a75eb03bSDavid Marchand server->shm_size = shm_size; 270a75eb03bSDavid Marchand server->n_vectors = n_vectors; 271a75eb03bSDavid Marchand 272a75eb03bSDavid Marchand QTAILQ_INIT(&server->peer_list); 273a75eb03bSDavid Marchand 274a75eb03bSDavid Marchand return 0; 275a75eb03bSDavid Marchand } 276a75eb03bSDavid Marchand 277*1e21feb6SMarc-André Lureau #ifdef CONFIG_LINUX 278*1e21feb6SMarc-André Lureau 279*1e21feb6SMarc-André Lureau #define HUGETLBFS_MAGIC 0x958458f6 280*1e21feb6SMarc-André Lureau 281*1e21feb6SMarc-André Lureau static long gethugepagesize(const char *path) 282*1e21feb6SMarc-André Lureau { 283*1e21feb6SMarc-André Lureau struct statfs fs; 284*1e21feb6SMarc-André Lureau int ret; 285*1e21feb6SMarc-André Lureau 286*1e21feb6SMarc-André Lureau do { 287*1e21feb6SMarc-André Lureau ret = statfs(path, &fs); 288*1e21feb6SMarc-André Lureau } while (ret != 0 && errno == EINTR); 289*1e21feb6SMarc-André Lureau 290*1e21feb6SMarc-André Lureau if (ret != 0) { 291*1e21feb6SMarc-André Lureau return -1; 292*1e21feb6SMarc-André Lureau } 293*1e21feb6SMarc-André Lureau 294*1e21feb6SMarc-André Lureau if (fs.f_type != HUGETLBFS_MAGIC) { 295*1e21feb6SMarc-André Lureau return -1; 296*1e21feb6SMarc-André Lureau } 297*1e21feb6SMarc-André Lureau 298*1e21feb6SMarc-André Lureau return fs.f_bsize; 299*1e21feb6SMarc-André Lureau } 300*1e21feb6SMarc-André Lureau #endif 301*1e21feb6SMarc-André Lureau 302a75eb03bSDavid Marchand /* open shm, create and bind to the unix socket */ 303a75eb03bSDavid Marchand int 304a75eb03bSDavid Marchand ivshmem_server_start(IvshmemServer *server) 305a75eb03bSDavid Marchand { 306a75eb03bSDavid Marchand struct sockaddr_un sun; 307a75eb03bSDavid Marchand int shm_fd, sock_fd, ret; 308a75eb03bSDavid Marchand 309a75eb03bSDavid Marchand /* open shm file */ 310*1e21feb6SMarc-André Lureau #ifdef CONFIG_LINUX 311*1e21feb6SMarc-André Lureau long hpagesize; 312*1e21feb6SMarc-André Lureau 313*1e21feb6SMarc-André Lureau hpagesize = gethugepagesize(server->shm_path); 314*1e21feb6SMarc-André Lureau if (hpagesize < 0 && errno != ENOENT) { 315*1e21feb6SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "cannot stat shm file %s: %s\n", 316*1e21feb6SMarc-André Lureau server->shm_path, strerror(errno)); 317*1e21feb6SMarc-André Lureau } 318*1e21feb6SMarc-André Lureau 319*1e21feb6SMarc-André Lureau if (hpagesize > 0) { 320*1e21feb6SMarc-André Lureau gchar *filename = g_strdup_printf("%s/ivshmem.XXXXXX", server->shm_path); 321*1e21feb6SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "Using hugepages: %s\n", server->shm_path); 322*1e21feb6SMarc-André Lureau shm_fd = mkstemp(filename); 323*1e21feb6SMarc-André Lureau unlink(filename); 324*1e21feb6SMarc-André Lureau g_free(filename); 325*1e21feb6SMarc-André Lureau } else 326*1e21feb6SMarc-André Lureau #endif 327*1e21feb6SMarc-André Lureau { 328*1e21feb6SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "Using POSIX shared memory: %s\n", 329*1e21feb6SMarc-André Lureau server->shm_path); 330a75eb03bSDavid Marchand shm_fd = shm_open(server->shm_path, O_CREAT|O_RDWR, S_IRWXU); 331*1e21feb6SMarc-André Lureau } 332*1e21feb6SMarc-André Lureau 333a75eb03bSDavid Marchand if (shm_fd < 0) { 334a75eb03bSDavid Marchand fprintf(stderr, "cannot open shm file %s: %s\n", server->shm_path, 335a75eb03bSDavid Marchand strerror(errno)); 336a75eb03bSDavid Marchand return -1; 337a75eb03bSDavid Marchand } 338a75eb03bSDavid Marchand if (ivshmem_server_ftruncate(shm_fd, server->shm_size) < 0) { 339a75eb03bSDavid Marchand fprintf(stderr, "ftruncate(%s) failed: %s\n", server->shm_path, 340a75eb03bSDavid Marchand strerror(errno)); 341a75eb03bSDavid Marchand goto err_close_shm; 342a75eb03bSDavid Marchand } 343a75eb03bSDavid Marchand 344a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "create & bind socket %s\n", 345a75eb03bSDavid Marchand server->unix_sock_path); 346a75eb03bSDavid Marchand 347a75eb03bSDavid Marchand /* create the unix listening socket */ 348a75eb03bSDavid Marchand sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); 349a75eb03bSDavid Marchand if (sock_fd < 0) { 350a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot create socket: %s\n", 351a75eb03bSDavid Marchand strerror(errno)); 352a75eb03bSDavid Marchand goto err_close_shm; 353a75eb03bSDavid Marchand } 354a75eb03bSDavid Marchand 355a75eb03bSDavid Marchand sun.sun_family = AF_UNIX; 356a75eb03bSDavid Marchand ret = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", 357a75eb03bSDavid Marchand server->unix_sock_path); 358a75eb03bSDavid Marchand if (ret < 0 || ret >= sizeof(sun.sun_path)) { 359a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "could not copy unix socket path\n"); 360a75eb03bSDavid Marchand goto err_close_sock; 361a75eb03bSDavid Marchand } 362a75eb03bSDavid Marchand if (bind(sock_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) { 363a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot connect to %s: %s\n", sun.sun_path, 364a75eb03bSDavid Marchand strerror(errno)); 365a75eb03bSDavid Marchand goto err_close_sock; 366a75eb03bSDavid Marchand } 367a75eb03bSDavid Marchand 368a75eb03bSDavid Marchand if (listen(sock_fd, IVSHMEM_SERVER_LISTEN_BACKLOG) < 0) { 369a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "listen() failed: %s\n", strerror(errno)); 370a75eb03bSDavid Marchand goto err_close_sock; 371a75eb03bSDavid Marchand } 372a75eb03bSDavid Marchand 373a75eb03bSDavid Marchand server->sock_fd = sock_fd; 374a75eb03bSDavid Marchand server->shm_fd = shm_fd; 375a75eb03bSDavid Marchand 376a75eb03bSDavid Marchand return 0; 377a75eb03bSDavid Marchand 378a75eb03bSDavid Marchand err_close_sock: 379a75eb03bSDavid Marchand close(sock_fd); 380a75eb03bSDavid Marchand err_close_shm: 381a75eb03bSDavid Marchand close(shm_fd); 382a75eb03bSDavid Marchand return -1; 383a75eb03bSDavid Marchand } 384a75eb03bSDavid Marchand 385a75eb03bSDavid Marchand /* close connections to clients, the unix socket and the shm fd */ 386a75eb03bSDavid Marchand void 387a75eb03bSDavid Marchand ivshmem_server_close(IvshmemServer *server) 388a75eb03bSDavid Marchand { 389a75eb03bSDavid Marchand IvshmemServerPeer *peer, *npeer; 390a75eb03bSDavid Marchand 391a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "close server\n"); 392a75eb03bSDavid Marchand 393a75eb03bSDavid Marchand QTAILQ_FOREACH_SAFE(peer, &server->peer_list, next, npeer) { 394a75eb03bSDavid Marchand ivshmem_server_free_peer(server, peer); 395a75eb03bSDavid Marchand } 396a75eb03bSDavid Marchand 397a75eb03bSDavid Marchand unlink(server->unix_sock_path); 398a75eb03bSDavid Marchand close(server->sock_fd); 399a75eb03bSDavid Marchand close(server->shm_fd); 400a75eb03bSDavid Marchand server->sock_fd = -1; 401a75eb03bSDavid Marchand server->shm_fd = -1; 402a75eb03bSDavid Marchand } 403a75eb03bSDavid Marchand 404a75eb03bSDavid Marchand /* get the fd_set according to the unix socket and the peer list */ 405a75eb03bSDavid Marchand void 406a75eb03bSDavid Marchand ivshmem_server_get_fds(const IvshmemServer *server, fd_set *fds, int *maxfd) 407a75eb03bSDavid Marchand { 408a75eb03bSDavid Marchand IvshmemServerPeer *peer; 409a75eb03bSDavid Marchand 410a75eb03bSDavid Marchand if (server->sock_fd == -1) { 411a75eb03bSDavid Marchand return; 412a75eb03bSDavid Marchand } 413a75eb03bSDavid Marchand 414a75eb03bSDavid Marchand FD_SET(server->sock_fd, fds); 415a75eb03bSDavid Marchand if (server->sock_fd >= *maxfd) { 416a75eb03bSDavid Marchand *maxfd = server->sock_fd + 1; 417a75eb03bSDavid Marchand } 418a75eb03bSDavid Marchand 419a75eb03bSDavid Marchand QTAILQ_FOREACH(peer, &server->peer_list, next) { 420a75eb03bSDavid Marchand FD_SET(peer->sock_fd, fds); 421a75eb03bSDavid Marchand if (peer->sock_fd >= *maxfd) { 422a75eb03bSDavid Marchand *maxfd = peer->sock_fd + 1; 423a75eb03bSDavid Marchand } 424a75eb03bSDavid Marchand } 425a75eb03bSDavid Marchand } 426a75eb03bSDavid Marchand 427a75eb03bSDavid Marchand /* process incoming messages on the sockets in fd_set */ 428a75eb03bSDavid Marchand int 429a75eb03bSDavid Marchand ivshmem_server_handle_fds(IvshmemServer *server, fd_set *fds, int maxfd) 430a75eb03bSDavid Marchand { 431a75eb03bSDavid Marchand IvshmemServerPeer *peer, *peer_next; 432a75eb03bSDavid Marchand 433a75eb03bSDavid Marchand if (server->sock_fd < maxfd && FD_ISSET(server->sock_fd, fds) && 434a75eb03bSDavid Marchand ivshmem_server_handle_new_conn(server) < 0 && errno != EINTR) { 435a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "ivshmem_server_handle_new_conn() " 436a75eb03bSDavid Marchand "failed\n"); 437a75eb03bSDavid Marchand return -1; 438a75eb03bSDavid Marchand } 439a75eb03bSDavid Marchand 440a75eb03bSDavid Marchand QTAILQ_FOREACH_SAFE(peer, &server->peer_list, next, peer_next) { 441a75eb03bSDavid Marchand /* any message from a peer socket result in a close() */ 442a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "peer->sock_fd=%d\n", peer->sock_fd); 443a75eb03bSDavid Marchand if (peer->sock_fd < maxfd && FD_ISSET(peer->sock_fd, fds)) { 444a75eb03bSDavid Marchand ivshmem_server_free_peer(server, peer); 445a75eb03bSDavid Marchand } 446a75eb03bSDavid Marchand } 447a75eb03bSDavid Marchand 448a75eb03bSDavid Marchand return 0; 449a75eb03bSDavid Marchand } 450a75eb03bSDavid Marchand 451a75eb03bSDavid Marchand /* lookup peer from its id */ 452a75eb03bSDavid Marchand IvshmemServerPeer * 453a75eb03bSDavid Marchand ivshmem_server_search_peer(IvshmemServer *server, long peer_id) 454a75eb03bSDavid Marchand { 455a75eb03bSDavid Marchand IvshmemServerPeer *peer; 456a75eb03bSDavid Marchand 457a75eb03bSDavid Marchand QTAILQ_FOREACH(peer, &server->peer_list, next) { 458a75eb03bSDavid Marchand if (peer->id == peer_id) { 459a75eb03bSDavid Marchand return peer; 460a75eb03bSDavid Marchand } 461a75eb03bSDavid Marchand } 462a75eb03bSDavid Marchand return NULL; 463a75eb03bSDavid Marchand } 464a75eb03bSDavid Marchand 465a75eb03bSDavid Marchand /* dump our info, the list of peers their vectors on stdout */ 466a75eb03bSDavid Marchand void 467a75eb03bSDavid Marchand ivshmem_server_dump(const IvshmemServer *server) 468a75eb03bSDavid Marchand { 469a75eb03bSDavid Marchand const IvshmemServerPeer *peer; 470a75eb03bSDavid Marchand unsigned vector; 471a75eb03bSDavid Marchand 472a75eb03bSDavid Marchand /* dump peers */ 473a75eb03bSDavid Marchand QTAILQ_FOREACH(peer, &server->peer_list, next) { 474a75eb03bSDavid Marchand printf("peer_id = %ld\n", peer->id); 475a75eb03bSDavid Marchand 476a75eb03bSDavid Marchand for (vector = 0; vector < peer->vectors_count; vector++) { 477a75eb03bSDavid Marchand printf(" vector %d is enabled (fd=%d)\n", vector, 478a75eb03bSDavid Marchand event_notifier_get_fd(&peer->vectors[vector])); 479a75eb03bSDavid Marchand } 480a75eb03bSDavid Marchand } 481a75eb03bSDavid Marchand } 482