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> 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 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 104*5105b1d8SDavid Marchand /* send our protocol version first */ 105*5105b1d8SDavid Marchand ret = ivshmem_server_send_one_msg(peer->sock_fd, IVSHMEM_PROTOCOL_VERSION, 106*5105b1d8SDavid Marchand -1); 107*5105b1d8SDavid Marchand if (ret < 0) { 108*5105b1d8SDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send version: %s\n", 109*5105b1d8SDavid Marchand strerror(errno)); 110*5105b1d8SDavid Marchand return -1; 111*5105b1d8SDavid Marchand } 112*5105b1d8SDavid Marchand 113a75eb03bSDavid Marchand /* send the peer id to the client */ 114a75eb03bSDavid Marchand ret = ivshmem_server_send_one_msg(peer->sock_fd, peer->id, -1); 115a75eb03bSDavid Marchand if (ret < 0) { 116a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send peer id: %s\n", 117a75eb03bSDavid Marchand strerror(errno)); 118a75eb03bSDavid Marchand return -1; 119a75eb03bSDavid Marchand } 120a75eb03bSDavid Marchand 121a75eb03bSDavid Marchand /* send the shm_fd */ 122a75eb03bSDavid Marchand ret = ivshmem_server_send_one_msg(peer->sock_fd, -1, server->shm_fd); 123a75eb03bSDavid Marchand if (ret < 0) { 124a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send shm fd: %s\n", 125a75eb03bSDavid Marchand strerror(errno)); 126a75eb03bSDavid Marchand return -1; 127a75eb03bSDavid Marchand } 128a75eb03bSDavid Marchand 129a75eb03bSDavid Marchand return 0; 130a75eb03bSDavid Marchand } 131a75eb03bSDavid Marchand 132a75eb03bSDavid Marchand /* handle message on listening unix socket (new client connection) */ 133a75eb03bSDavid Marchand static int 134a75eb03bSDavid Marchand ivshmem_server_handle_new_conn(IvshmemServer *server) 135a75eb03bSDavid Marchand { 136a75eb03bSDavid Marchand IvshmemServerPeer *peer, *other_peer; 137a75eb03bSDavid Marchand struct sockaddr_un unaddr; 138a75eb03bSDavid Marchand socklen_t unaddr_len; 139a75eb03bSDavid Marchand int newfd; 140a75eb03bSDavid Marchand unsigned i; 141a75eb03bSDavid Marchand 142a75eb03bSDavid Marchand /* accept the incoming connection */ 143a75eb03bSDavid Marchand unaddr_len = sizeof(unaddr); 144a75eb03bSDavid Marchand newfd = qemu_accept(server->sock_fd, 145a75eb03bSDavid Marchand (struct sockaddr *)&unaddr, &unaddr_len); 146a75eb03bSDavid Marchand 147a75eb03bSDavid Marchand if (newfd < 0) { 148a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot accept() %s\n", strerror(errno)); 149a75eb03bSDavid Marchand return -1; 150a75eb03bSDavid Marchand } 151a75eb03bSDavid Marchand 152a75eb03bSDavid Marchand qemu_set_nonblock(newfd); 153a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "accept()=%d\n", newfd); 154a75eb03bSDavid Marchand 155a75eb03bSDavid Marchand /* allocate new structure for this peer */ 156a75eb03bSDavid Marchand peer = g_malloc0(sizeof(*peer)); 157a75eb03bSDavid Marchand peer->sock_fd = newfd; 158a75eb03bSDavid Marchand 159a75eb03bSDavid Marchand /* get an unused peer id */ 160022cffe3SMarc-André Lureau /* XXX: this could use id allocation such as Linux IDA, or simply 161022cffe3SMarc-André Lureau * a free-list */ 162022cffe3SMarc-André Lureau for (i = 0; i < G_MAXUINT16; i++) { 163022cffe3SMarc-André Lureau if (ivshmem_server_search_peer(server, server->cur_id) == NULL) { 164022cffe3SMarc-André Lureau break; 165022cffe3SMarc-André Lureau } 166a75eb03bSDavid Marchand server->cur_id++; 167a75eb03bSDavid Marchand } 168022cffe3SMarc-André Lureau if (i == G_MAXUINT16) { 169022cffe3SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "cannot allocate new client id\n"); 170022cffe3SMarc-André Lureau goto fail; 171022cffe3SMarc-André Lureau } 172a75eb03bSDavid Marchand peer->id = server->cur_id++; 173a75eb03bSDavid Marchand 174a75eb03bSDavid Marchand /* create eventfd, one per vector */ 175a75eb03bSDavid Marchand peer->vectors_count = server->n_vectors; 176a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 177a75eb03bSDavid Marchand if (event_notifier_init(&peer->vectors[i], FALSE) < 0) { 178a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot create eventfd\n"); 179a75eb03bSDavid Marchand goto fail; 180a75eb03bSDavid Marchand } 181a75eb03bSDavid Marchand } 182a75eb03bSDavid Marchand 183a75eb03bSDavid Marchand /* send peer id and shm fd */ 184a75eb03bSDavid Marchand if (ivshmem_server_send_initial_info(server, peer) < 0) { 185a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send initial info\n"); 186a75eb03bSDavid Marchand goto fail; 187a75eb03bSDavid Marchand } 188a75eb03bSDavid Marchand 189a75eb03bSDavid Marchand /* advertise the new peer to others */ 190a75eb03bSDavid Marchand QTAILQ_FOREACH(other_peer, &server->peer_list, next) { 191a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 192a75eb03bSDavid Marchand ivshmem_server_send_one_msg(other_peer->sock_fd, peer->id, 193a75eb03bSDavid Marchand peer->vectors[i].wfd); 194a75eb03bSDavid Marchand } 195a75eb03bSDavid Marchand } 196a75eb03bSDavid Marchand 197a75eb03bSDavid Marchand /* advertise the other peers to the new one */ 198a75eb03bSDavid Marchand QTAILQ_FOREACH(other_peer, &server->peer_list, next) { 199a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 200a75eb03bSDavid Marchand ivshmem_server_send_one_msg(peer->sock_fd, other_peer->id, 201a75eb03bSDavid Marchand other_peer->vectors[i].wfd); 202a75eb03bSDavid Marchand } 203a75eb03bSDavid Marchand } 204a75eb03bSDavid Marchand 205a75eb03bSDavid Marchand /* advertise the new peer to itself */ 206a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 207a75eb03bSDavid Marchand ivshmem_server_send_one_msg(peer->sock_fd, peer->id, 208a75eb03bSDavid Marchand event_notifier_get_fd(&peer->vectors[i])); 209a75eb03bSDavid Marchand } 210a75eb03bSDavid Marchand 211a75eb03bSDavid Marchand QTAILQ_INSERT_TAIL(&server->peer_list, peer, next); 212a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "new peer id = %ld\n", 213a75eb03bSDavid Marchand peer->id); 214a75eb03bSDavid Marchand return 0; 215a75eb03bSDavid Marchand 216a75eb03bSDavid Marchand fail: 217a75eb03bSDavid Marchand while (i--) { 218a75eb03bSDavid Marchand event_notifier_cleanup(&peer->vectors[i]); 219a75eb03bSDavid Marchand } 220a75eb03bSDavid Marchand close(newfd); 221a75eb03bSDavid Marchand g_free(peer); 222a75eb03bSDavid Marchand return -1; 223a75eb03bSDavid Marchand } 224a75eb03bSDavid Marchand 225a75eb03bSDavid Marchand /* Try to ftruncate a file to next power of 2 of shmsize. 226a75eb03bSDavid Marchand * If it fails; all power of 2 above shmsize are tested until 227a75eb03bSDavid Marchand * we reach the maximum huge page size. This is useful 228a75eb03bSDavid Marchand * if the shm file is in a hugetlbfs that cannot be truncated to the 229a75eb03bSDavid Marchand * shm_size value. */ 230a75eb03bSDavid Marchand static int 231a75eb03bSDavid Marchand ivshmem_server_ftruncate(int fd, unsigned shmsize) 232a75eb03bSDavid Marchand { 233a75eb03bSDavid Marchand int ret; 234a75eb03bSDavid Marchand struct stat mapstat; 235a75eb03bSDavid Marchand 236a75eb03bSDavid Marchand /* align shmsize to next power of 2 */ 237a75eb03bSDavid Marchand shmsize = pow2ceil(shmsize); 238a75eb03bSDavid Marchand 239a75eb03bSDavid Marchand if (fstat(fd, &mapstat) != -1 && mapstat.st_size == shmsize) { 240a75eb03bSDavid Marchand return 0; 241a75eb03bSDavid Marchand } 242a75eb03bSDavid Marchand 243a75eb03bSDavid Marchand while (shmsize <= IVSHMEM_SERVER_MAX_HUGEPAGE_SIZE) { 244a75eb03bSDavid Marchand ret = ftruncate(fd, shmsize); 245a75eb03bSDavid Marchand if (ret == 0) { 246a75eb03bSDavid Marchand return ret; 247a75eb03bSDavid Marchand } 248a75eb03bSDavid Marchand shmsize *= 2; 249a75eb03bSDavid Marchand } 250a75eb03bSDavid Marchand 251a75eb03bSDavid Marchand return -1; 252a75eb03bSDavid Marchand } 253a75eb03bSDavid Marchand 254a75eb03bSDavid Marchand /* Init a new ivshmem server */ 255a75eb03bSDavid Marchand int 256a75eb03bSDavid Marchand ivshmem_server_init(IvshmemServer *server, const char *unix_sock_path, 257a75eb03bSDavid Marchand const char *shm_path, size_t shm_size, unsigned n_vectors, 258a75eb03bSDavid Marchand bool verbose) 259a75eb03bSDavid Marchand { 260a75eb03bSDavid Marchand int ret; 261a75eb03bSDavid Marchand 262a75eb03bSDavid Marchand memset(server, 0, sizeof(*server)); 263a75eb03bSDavid Marchand server->verbose = verbose; 264a75eb03bSDavid Marchand 265a75eb03bSDavid Marchand ret = snprintf(server->unix_sock_path, sizeof(server->unix_sock_path), 266a75eb03bSDavid Marchand "%s", unix_sock_path); 267a75eb03bSDavid Marchand if (ret < 0 || ret >= sizeof(server->unix_sock_path)) { 268a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "could not copy unix socket path\n"); 269a75eb03bSDavid Marchand return -1; 270a75eb03bSDavid Marchand } 271a75eb03bSDavid Marchand ret = snprintf(server->shm_path, sizeof(server->shm_path), 272a75eb03bSDavid Marchand "%s", shm_path); 273a75eb03bSDavid Marchand if (ret < 0 || ret >= sizeof(server->shm_path)) { 274a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "could not copy shm path\n"); 275a75eb03bSDavid Marchand return -1; 276a75eb03bSDavid Marchand } 277a75eb03bSDavid Marchand 278a75eb03bSDavid Marchand server->shm_size = shm_size; 279a75eb03bSDavid Marchand server->n_vectors = n_vectors; 280a75eb03bSDavid Marchand 281a75eb03bSDavid Marchand QTAILQ_INIT(&server->peer_list); 282a75eb03bSDavid Marchand 283a75eb03bSDavid Marchand return 0; 284a75eb03bSDavid Marchand } 285a75eb03bSDavid Marchand 2861e21feb6SMarc-André Lureau #ifdef CONFIG_LINUX 2871e21feb6SMarc-André Lureau 2881e21feb6SMarc-André Lureau #define HUGETLBFS_MAGIC 0x958458f6 2891e21feb6SMarc-André Lureau 2901e21feb6SMarc-André Lureau static long gethugepagesize(const char *path) 2911e21feb6SMarc-André Lureau { 2921e21feb6SMarc-André Lureau struct statfs fs; 2931e21feb6SMarc-André Lureau int ret; 2941e21feb6SMarc-André Lureau 2951e21feb6SMarc-André Lureau do { 2961e21feb6SMarc-André Lureau ret = statfs(path, &fs); 2971e21feb6SMarc-André Lureau } while (ret != 0 && errno == EINTR); 2981e21feb6SMarc-André Lureau 2991e21feb6SMarc-André Lureau if (ret != 0) { 3001e21feb6SMarc-André Lureau return -1; 3011e21feb6SMarc-André Lureau } 3021e21feb6SMarc-André Lureau 3031e21feb6SMarc-André Lureau if (fs.f_type != HUGETLBFS_MAGIC) { 3041e21feb6SMarc-André Lureau return -1; 3051e21feb6SMarc-André Lureau } 3061e21feb6SMarc-André Lureau 3071e21feb6SMarc-André Lureau return fs.f_bsize; 3081e21feb6SMarc-André Lureau } 3091e21feb6SMarc-André Lureau #endif 3101e21feb6SMarc-André Lureau 311a75eb03bSDavid Marchand /* open shm, create and bind to the unix socket */ 312a75eb03bSDavid Marchand int 313a75eb03bSDavid Marchand ivshmem_server_start(IvshmemServer *server) 314a75eb03bSDavid Marchand { 315a75eb03bSDavid Marchand struct sockaddr_un sun; 316a75eb03bSDavid Marchand int shm_fd, sock_fd, ret; 317a75eb03bSDavid Marchand 318a75eb03bSDavid Marchand /* open shm file */ 3191e21feb6SMarc-André Lureau #ifdef CONFIG_LINUX 3201e21feb6SMarc-André Lureau long hpagesize; 3211e21feb6SMarc-André Lureau 3221e21feb6SMarc-André Lureau hpagesize = gethugepagesize(server->shm_path); 3231e21feb6SMarc-André Lureau if (hpagesize < 0 && errno != ENOENT) { 3241e21feb6SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "cannot stat shm file %s: %s\n", 3251e21feb6SMarc-André Lureau server->shm_path, strerror(errno)); 3261e21feb6SMarc-André Lureau } 3271e21feb6SMarc-André Lureau 3281e21feb6SMarc-André Lureau if (hpagesize > 0) { 3291e21feb6SMarc-André Lureau gchar *filename = g_strdup_printf("%s/ivshmem.XXXXXX", server->shm_path); 3301e21feb6SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "Using hugepages: %s\n", server->shm_path); 3311e21feb6SMarc-André Lureau shm_fd = mkstemp(filename); 3321e21feb6SMarc-André Lureau unlink(filename); 3331e21feb6SMarc-André Lureau g_free(filename); 3341e21feb6SMarc-André Lureau } else 3351e21feb6SMarc-André Lureau #endif 3361e21feb6SMarc-André Lureau { 3371e21feb6SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "Using POSIX shared memory: %s\n", 3381e21feb6SMarc-André Lureau server->shm_path); 339a75eb03bSDavid Marchand shm_fd = shm_open(server->shm_path, O_CREAT|O_RDWR, S_IRWXU); 3401e21feb6SMarc-André Lureau } 3411e21feb6SMarc-André Lureau 342a75eb03bSDavid Marchand if (shm_fd < 0) { 343a75eb03bSDavid Marchand fprintf(stderr, "cannot open shm file %s: %s\n", server->shm_path, 344a75eb03bSDavid Marchand strerror(errno)); 345a75eb03bSDavid Marchand return -1; 346a75eb03bSDavid Marchand } 347a75eb03bSDavid Marchand if (ivshmem_server_ftruncate(shm_fd, server->shm_size) < 0) { 348a75eb03bSDavid Marchand fprintf(stderr, "ftruncate(%s) failed: %s\n", server->shm_path, 349a75eb03bSDavid Marchand strerror(errno)); 350a75eb03bSDavid Marchand goto err_close_shm; 351a75eb03bSDavid Marchand } 352a75eb03bSDavid Marchand 353a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "create & bind socket %s\n", 354a75eb03bSDavid Marchand server->unix_sock_path); 355a75eb03bSDavid Marchand 356a75eb03bSDavid Marchand /* create the unix listening socket */ 357a75eb03bSDavid Marchand sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); 358a75eb03bSDavid Marchand if (sock_fd < 0) { 359a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot create socket: %s\n", 360a75eb03bSDavid Marchand strerror(errno)); 361a75eb03bSDavid Marchand goto err_close_shm; 362a75eb03bSDavid Marchand } 363a75eb03bSDavid Marchand 364a75eb03bSDavid Marchand sun.sun_family = AF_UNIX; 365a75eb03bSDavid Marchand ret = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", 366a75eb03bSDavid Marchand server->unix_sock_path); 367a75eb03bSDavid Marchand if (ret < 0 || ret >= sizeof(sun.sun_path)) { 368a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "could not copy unix socket path\n"); 369a75eb03bSDavid Marchand goto err_close_sock; 370a75eb03bSDavid Marchand } 371a75eb03bSDavid Marchand if (bind(sock_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) { 372a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot connect to %s: %s\n", sun.sun_path, 373a75eb03bSDavid Marchand strerror(errno)); 374a75eb03bSDavid Marchand goto err_close_sock; 375a75eb03bSDavid Marchand } 376a75eb03bSDavid Marchand 377a75eb03bSDavid Marchand if (listen(sock_fd, IVSHMEM_SERVER_LISTEN_BACKLOG) < 0) { 378a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "listen() failed: %s\n", strerror(errno)); 379a75eb03bSDavid Marchand goto err_close_sock; 380a75eb03bSDavid Marchand } 381a75eb03bSDavid Marchand 382a75eb03bSDavid Marchand server->sock_fd = sock_fd; 383a75eb03bSDavid Marchand server->shm_fd = shm_fd; 384a75eb03bSDavid Marchand 385a75eb03bSDavid Marchand return 0; 386a75eb03bSDavid Marchand 387a75eb03bSDavid Marchand err_close_sock: 388a75eb03bSDavid Marchand close(sock_fd); 389a75eb03bSDavid Marchand err_close_shm: 390a75eb03bSDavid Marchand close(shm_fd); 391a75eb03bSDavid Marchand return -1; 392a75eb03bSDavid Marchand } 393a75eb03bSDavid Marchand 394a75eb03bSDavid Marchand /* close connections to clients, the unix socket and the shm fd */ 395a75eb03bSDavid Marchand void 396a75eb03bSDavid Marchand ivshmem_server_close(IvshmemServer *server) 397a75eb03bSDavid Marchand { 398a75eb03bSDavid Marchand IvshmemServerPeer *peer, *npeer; 399a75eb03bSDavid Marchand 400a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "close server\n"); 401a75eb03bSDavid Marchand 402a75eb03bSDavid Marchand QTAILQ_FOREACH_SAFE(peer, &server->peer_list, next, npeer) { 403a75eb03bSDavid Marchand ivshmem_server_free_peer(server, peer); 404a75eb03bSDavid Marchand } 405a75eb03bSDavid Marchand 406a75eb03bSDavid Marchand unlink(server->unix_sock_path); 407a75eb03bSDavid Marchand close(server->sock_fd); 408a75eb03bSDavid Marchand close(server->shm_fd); 409a75eb03bSDavid Marchand server->sock_fd = -1; 410a75eb03bSDavid Marchand server->shm_fd = -1; 411a75eb03bSDavid Marchand } 412a75eb03bSDavid Marchand 413a75eb03bSDavid Marchand /* get the fd_set according to the unix socket and the peer list */ 414a75eb03bSDavid Marchand void 415a75eb03bSDavid Marchand ivshmem_server_get_fds(const IvshmemServer *server, fd_set *fds, int *maxfd) 416a75eb03bSDavid Marchand { 417a75eb03bSDavid Marchand IvshmemServerPeer *peer; 418a75eb03bSDavid Marchand 419a75eb03bSDavid Marchand if (server->sock_fd == -1) { 420a75eb03bSDavid Marchand return; 421a75eb03bSDavid Marchand } 422a75eb03bSDavid Marchand 423a75eb03bSDavid Marchand FD_SET(server->sock_fd, fds); 424a75eb03bSDavid Marchand if (server->sock_fd >= *maxfd) { 425a75eb03bSDavid Marchand *maxfd = server->sock_fd + 1; 426a75eb03bSDavid Marchand } 427a75eb03bSDavid Marchand 428a75eb03bSDavid Marchand QTAILQ_FOREACH(peer, &server->peer_list, next) { 429a75eb03bSDavid Marchand FD_SET(peer->sock_fd, fds); 430a75eb03bSDavid Marchand if (peer->sock_fd >= *maxfd) { 431a75eb03bSDavid Marchand *maxfd = peer->sock_fd + 1; 432a75eb03bSDavid Marchand } 433a75eb03bSDavid Marchand } 434a75eb03bSDavid Marchand } 435a75eb03bSDavid Marchand 436a75eb03bSDavid Marchand /* process incoming messages on the sockets in fd_set */ 437a75eb03bSDavid Marchand int 438a75eb03bSDavid Marchand ivshmem_server_handle_fds(IvshmemServer *server, fd_set *fds, int maxfd) 439a75eb03bSDavid Marchand { 440a75eb03bSDavid Marchand IvshmemServerPeer *peer, *peer_next; 441a75eb03bSDavid Marchand 442a75eb03bSDavid Marchand if (server->sock_fd < maxfd && FD_ISSET(server->sock_fd, fds) && 443a75eb03bSDavid Marchand ivshmem_server_handle_new_conn(server) < 0 && errno != EINTR) { 444a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "ivshmem_server_handle_new_conn() " 445a75eb03bSDavid Marchand "failed\n"); 446a75eb03bSDavid Marchand return -1; 447a75eb03bSDavid Marchand } 448a75eb03bSDavid Marchand 449a75eb03bSDavid Marchand QTAILQ_FOREACH_SAFE(peer, &server->peer_list, next, peer_next) { 450a75eb03bSDavid Marchand /* any message from a peer socket result in a close() */ 451a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "peer->sock_fd=%d\n", peer->sock_fd); 452a75eb03bSDavid Marchand if (peer->sock_fd < maxfd && FD_ISSET(peer->sock_fd, fds)) { 453a75eb03bSDavid Marchand ivshmem_server_free_peer(server, peer); 454a75eb03bSDavid Marchand } 455a75eb03bSDavid Marchand } 456a75eb03bSDavid Marchand 457a75eb03bSDavid Marchand return 0; 458a75eb03bSDavid Marchand } 459a75eb03bSDavid Marchand 460a75eb03bSDavid Marchand /* lookup peer from its id */ 461a75eb03bSDavid Marchand IvshmemServerPeer * 462a75eb03bSDavid Marchand ivshmem_server_search_peer(IvshmemServer *server, long peer_id) 463a75eb03bSDavid Marchand { 464a75eb03bSDavid Marchand IvshmemServerPeer *peer; 465a75eb03bSDavid Marchand 466a75eb03bSDavid Marchand QTAILQ_FOREACH(peer, &server->peer_list, next) { 467a75eb03bSDavid Marchand if (peer->id == peer_id) { 468a75eb03bSDavid Marchand return peer; 469a75eb03bSDavid Marchand } 470a75eb03bSDavid Marchand } 471a75eb03bSDavid Marchand return NULL; 472a75eb03bSDavid Marchand } 473a75eb03bSDavid Marchand 474a75eb03bSDavid Marchand /* dump our info, the list of peers their vectors on stdout */ 475a75eb03bSDavid Marchand void 476a75eb03bSDavid Marchand ivshmem_server_dump(const IvshmemServer *server) 477a75eb03bSDavid Marchand { 478a75eb03bSDavid Marchand const IvshmemServerPeer *peer; 479a75eb03bSDavid Marchand unsigned vector; 480a75eb03bSDavid Marchand 481a75eb03bSDavid Marchand /* dump peers */ 482a75eb03bSDavid Marchand QTAILQ_FOREACH(peer, &server->peer_list, next) { 483a75eb03bSDavid Marchand printf("peer_id = %ld\n", peer->id); 484a75eb03bSDavid Marchand 485a75eb03bSDavid Marchand for (vector = 0; vector < peer->vectors_count; vector++) { 486a75eb03bSDavid Marchand printf(" vector %d is enabled (fd=%d)\n", vector, 487a75eb03bSDavid Marchand event_notifier_get_fd(&peer->vectors[vector])); 488a75eb03bSDavid Marchand } 489a75eb03bSDavid Marchand } 490a75eb03bSDavid Marchand } 491