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 */ 8ccd241b5SPeter Maydell #include "qemu/osdep.h" 987776ab7SPaolo Bonzini #include "qemu/host-utils.h" 10a75eb03bSDavid Marchand #include "qemu/sockets.h" 11a75eb03bSDavid Marchand 12a75eb03bSDavid Marchand #include <sys/socket.h> 13a75eb03bSDavid Marchand #include <sys/un.h> 14a75eb03bSDavid Marchand 15a75eb03bSDavid Marchand #include "ivshmem-server.h" 16a75eb03bSDavid Marchand 17a75eb03bSDavid Marchand /* log a message on stdout if verbose=1 */ 18a75eb03bSDavid Marchand #define IVSHMEM_SERVER_DEBUG(server, fmt, ...) do { \ 19a75eb03bSDavid Marchand if ((server)->verbose) { \ 20a75eb03bSDavid Marchand printf(fmt, ## __VA_ARGS__); \ 21a75eb03bSDavid Marchand } \ 22a75eb03bSDavid Marchand } while (0) 23a75eb03bSDavid Marchand 24a75eb03bSDavid Marchand /** maximum size of a huge page, used by ivshmem_server_ftruncate() */ 25a75eb03bSDavid Marchand #define IVSHMEM_SERVER_MAX_HUGEPAGE_SIZE (1024 * 1024 * 1024) 26a75eb03bSDavid Marchand 27a75eb03bSDavid Marchand /** default listen backlog (number of sockets not accepted) */ 28a75eb03bSDavid Marchand #define IVSHMEM_SERVER_LISTEN_BACKLOG 10 29a75eb03bSDavid Marchand 30a75eb03bSDavid Marchand /* send message to a client unix socket */ 31a75eb03bSDavid Marchand static int 32f7a199b2SMarc-André Lureau ivshmem_server_send_one_msg(int sock_fd, int64_t peer_id, int fd) 33a75eb03bSDavid Marchand { 34a75eb03bSDavid Marchand int ret; 35a75eb03bSDavid Marchand struct msghdr msg; 36a75eb03bSDavid Marchand struct iovec iov[1]; 37a75eb03bSDavid Marchand union { 38a75eb03bSDavid Marchand struct cmsghdr cmsg; 39a75eb03bSDavid Marchand char control[CMSG_SPACE(sizeof(int))]; 40a75eb03bSDavid Marchand } msg_control; 41a75eb03bSDavid Marchand struct cmsghdr *cmsg; 42a75eb03bSDavid Marchand 43f7a199b2SMarc-André Lureau peer_id = GINT64_TO_LE(peer_id); 44a75eb03bSDavid Marchand iov[0].iov_base = &peer_id; 45a75eb03bSDavid Marchand iov[0].iov_len = sizeof(peer_id); 46a75eb03bSDavid Marchand 47a75eb03bSDavid Marchand memset(&msg, 0, sizeof(msg)); 48a75eb03bSDavid Marchand msg.msg_iov = iov; 49a75eb03bSDavid Marchand msg.msg_iovlen = 1; 50a75eb03bSDavid Marchand 51a75eb03bSDavid Marchand /* if fd is specified, add it in a cmsg */ 52a75eb03bSDavid Marchand if (fd >= 0) { 53a75eb03bSDavid Marchand memset(&msg_control, 0, sizeof(msg_control)); 54a75eb03bSDavid Marchand msg.msg_control = &msg_control; 55a75eb03bSDavid Marchand msg.msg_controllen = sizeof(msg_control); 56a75eb03bSDavid Marchand cmsg = CMSG_FIRSTHDR(&msg); 57a75eb03bSDavid Marchand cmsg->cmsg_level = SOL_SOCKET; 58a75eb03bSDavid Marchand cmsg->cmsg_type = SCM_RIGHTS; 59a75eb03bSDavid Marchand cmsg->cmsg_len = CMSG_LEN(sizeof(int)); 60a75eb03bSDavid Marchand memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); 61a75eb03bSDavid Marchand } 62a75eb03bSDavid Marchand 63a75eb03bSDavid Marchand ret = sendmsg(sock_fd, &msg, 0); 64a75eb03bSDavid Marchand if (ret <= 0) { 65a75eb03bSDavid Marchand return -1; 66a75eb03bSDavid Marchand } 67a75eb03bSDavid Marchand 68a75eb03bSDavid Marchand return 0; 69a75eb03bSDavid Marchand } 70a75eb03bSDavid Marchand 71a75eb03bSDavid Marchand /* free a peer when the server advertises a disconnection or when the 72a75eb03bSDavid Marchand * server is freed */ 73a75eb03bSDavid Marchand static void 74a75eb03bSDavid Marchand ivshmem_server_free_peer(IvshmemServer *server, IvshmemServerPeer *peer) 75a75eb03bSDavid Marchand { 76a75eb03bSDavid Marchand unsigned vector; 77a75eb03bSDavid Marchand IvshmemServerPeer *other_peer; 78a75eb03bSDavid Marchand 79f7a199b2SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "free peer %" PRId64 "\n", peer->id); 80a75eb03bSDavid Marchand close(peer->sock_fd); 81a75eb03bSDavid Marchand QTAILQ_REMOVE(&server->peer_list, peer, next); 82a75eb03bSDavid Marchand 83a75eb03bSDavid Marchand /* advertise the deletion to other peers */ 84a75eb03bSDavid Marchand QTAILQ_FOREACH(other_peer, &server->peer_list, next) { 85a75eb03bSDavid Marchand ivshmem_server_send_one_msg(other_peer->sock_fd, peer->id, -1); 86a75eb03bSDavid Marchand } 87a75eb03bSDavid Marchand 88a75eb03bSDavid Marchand for (vector = 0; vector < peer->vectors_count; vector++) { 89a75eb03bSDavid Marchand event_notifier_cleanup(&peer->vectors[vector]); 90a75eb03bSDavid Marchand } 91a75eb03bSDavid Marchand 92a75eb03bSDavid Marchand g_free(peer); 93a75eb03bSDavid Marchand } 94a75eb03bSDavid Marchand 95a75eb03bSDavid Marchand /* send the peer id and the shm_fd just after a new client connection */ 96a75eb03bSDavid Marchand static int 97a75eb03bSDavid Marchand ivshmem_server_send_initial_info(IvshmemServer *server, IvshmemServerPeer *peer) 98a75eb03bSDavid Marchand { 99a75eb03bSDavid Marchand int ret; 100a75eb03bSDavid Marchand 1015105b1d8SDavid Marchand /* send our protocol version first */ 1025105b1d8SDavid Marchand ret = ivshmem_server_send_one_msg(peer->sock_fd, IVSHMEM_PROTOCOL_VERSION, 1035105b1d8SDavid Marchand -1); 1045105b1d8SDavid Marchand if (ret < 0) { 1055105b1d8SDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send version: %s\n", 1065105b1d8SDavid Marchand strerror(errno)); 1075105b1d8SDavid Marchand return -1; 1085105b1d8SDavid Marchand } 1095105b1d8SDavid Marchand 110a75eb03bSDavid Marchand /* send the peer id to the client */ 111a75eb03bSDavid Marchand ret = ivshmem_server_send_one_msg(peer->sock_fd, peer->id, -1); 112a75eb03bSDavid Marchand if (ret < 0) { 113a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send peer id: %s\n", 114a75eb03bSDavid Marchand strerror(errno)); 115a75eb03bSDavid Marchand return -1; 116a75eb03bSDavid Marchand } 117a75eb03bSDavid Marchand 118a75eb03bSDavid Marchand /* send the shm_fd */ 119a75eb03bSDavid Marchand ret = ivshmem_server_send_one_msg(peer->sock_fd, -1, server->shm_fd); 120a75eb03bSDavid Marchand if (ret < 0) { 121a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send shm fd: %s\n", 122a75eb03bSDavid Marchand strerror(errno)); 123a75eb03bSDavid Marchand return -1; 124a75eb03bSDavid Marchand } 125a75eb03bSDavid Marchand 126a75eb03bSDavid Marchand return 0; 127a75eb03bSDavid Marchand } 128a75eb03bSDavid Marchand 129a75eb03bSDavid Marchand /* handle message on listening unix socket (new client connection) */ 130a75eb03bSDavid Marchand static int 131a75eb03bSDavid Marchand ivshmem_server_handle_new_conn(IvshmemServer *server) 132a75eb03bSDavid Marchand { 133a75eb03bSDavid Marchand IvshmemServerPeer *peer, *other_peer; 134a75eb03bSDavid Marchand struct sockaddr_un unaddr; 135a75eb03bSDavid Marchand socklen_t unaddr_len; 136a75eb03bSDavid Marchand int newfd; 137a75eb03bSDavid Marchand unsigned i; 138a75eb03bSDavid Marchand 139a75eb03bSDavid Marchand /* accept the incoming connection */ 140a75eb03bSDavid Marchand unaddr_len = sizeof(unaddr); 141a75eb03bSDavid Marchand newfd = qemu_accept(server->sock_fd, 142a75eb03bSDavid Marchand (struct sockaddr *)&unaddr, &unaddr_len); 143a75eb03bSDavid Marchand 144a75eb03bSDavid Marchand if (newfd < 0) { 145a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot accept() %s\n", strerror(errno)); 146a75eb03bSDavid Marchand return -1; 147a75eb03bSDavid Marchand } 148a75eb03bSDavid Marchand 149a75eb03bSDavid Marchand qemu_set_nonblock(newfd); 150a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "accept()=%d\n", newfd); 151a75eb03bSDavid Marchand 152a75eb03bSDavid Marchand /* allocate new structure for this peer */ 153a75eb03bSDavid Marchand peer = g_malloc0(sizeof(*peer)); 154a75eb03bSDavid Marchand peer->sock_fd = newfd; 155a75eb03bSDavid Marchand 156a75eb03bSDavid Marchand /* get an unused peer id */ 157022cffe3SMarc-André Lureau /* XXX: this could use id allocation such as Linux IDA, or simply 158022cffe3SMarc-André Lureau * a free-list */ 159022cffe3SMarc-André Lureau for (i = 0; i < G_MAXUINT16; i++) { 160022cffe3SMarc-André Lureau if (ivshmem_server_search_peer(server, server->cur_id) == NULL) { 161022cffe3SMarc-André Lureau break; 162022cffe3SMarc-André Lureau } 163a75eb03bSDavid Marchand server->cur_id++; 164a75eb03bSDavid Marchand } 165022cffe3SMarc-André Lureau if (i == G_MAXUINT16) { 166022cffe3SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "cannot allocate new client id\n"); 167258133bdSGonglei close(newfd); 168258133bdSGonglei g_free(peer); 169258133bdSGonglei return -1; 170022cffe3SMarc-André Lureau } 171a75eb03bSDavid Marchand peer->id = server->cur_id++; 172a75eb03bSDavid Marchand 173a75eb03bSDavid Marchand /* create eventfd, one per vector */ 174a75eb03bSDavid Marchand peer->vectors_count = server->n_vectors; 175a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 176a75eb03bSDavid Marchand if (event_notifier_init(&peer->vectors[i], FALSE) < 0) { 177a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot create eventfd\n"); 178a75eb03bSDavid Marchand goto fail; 179a75eb03bSDavid Marchand } 180a75eb03bSDavid Marchand } 181a75eb03bSDavid Marchand 182a75eb03bSDavid Marchand /* send peer id and shm fd */ 183a75eb03bSDavid Marchand if (ivshmem_server_send_initial_info(server, peer) < 0) { 184a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot send initial info\n"); 185a75eb03bSDavid Marchand goto fail; 186a75eb03bSDavid Marchand } 187a75eb03bSDavid Marchand 188a75eb03bSDavid Marchand /* advertise the new peer to others */ 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(other_peer->sock_fd, peer->id, 192a75eb03bSDavid Marchand peer->vectors[i].wfd); 193a75eb03bSDavid Marchand } 194a75eb03bSDavid Marchand } 195a75eb03bSDavid Marchand 196a75eb03bSDavid Marchand /* advertise the other peers to the new one */ 197a75eb03bSDavid Marchand QTAILQ_FOREACH(other_peer, &server->peer_list, next) { 198a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 199a75eb03bSDavid Marchand ivshmem_server_send_one_msg(peer->sock_fd, other_peer->id, 200a75eb03bSDavid Marchand other_peer->vectors[i].wfd); 201a75eb03bSDavid Marchand } 202a75eb03bSDavid Marchand } 203a75eb03bSDavid Marchand 204a75eb03bSDavid Marchand /* advertise the new peer to itself */ 205a75eb03bSDavid Marchand for (i = 0; i < peer->vectors_count; i++) { 206a75eb03bSDavid Marchand ivshmem_server_send_one_msg(peer->sock_fd, peer->id, 207a75eb03bSDavid Marchand event_notifier_get_fd(&peer->vectors[i])); 208a75eb03bSDavid Marchand } 209a75eb03bSDavid Marchand 210a75eb03bSDavid Marchand QTAILQ_INSERT_TAIL(&server->peer_list, peer, next); 211f7a199b2SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "new peer id = %" PRId64 "\n", 212a75eb03bSDavid Marchand peer->id); 213a75eb03bSDavid Marchand return 0; 214a75eb03bSDavid Marchand 215a75eb03bSDavid Marchand fail: 216a75eb03bSDavid Marchand while (i--) { 217a75eb03bSDavid Marchand event_notifier_cleanup(&peer->vectors[i]); 218a75eb03bSDavid Marchand } 219a75eb03bSDavid Marchand close(newfd); 220a75eb03bSDavid Marchand g_free(peer); 221a75eb03bSDavid Marchand return -1; 222a75eb03bSDavid Marchand } 223a75eb03bSDavid Marchand 224a75eb03bSDavid Marchand /* Try to ftruncate a file to next power of 2 of shmsize. 225a75eb03bSDavid Marchand * If it fails; all power of 2 above shmsize are tested until 226a75eb03bSDavid Marchand * we reach the maximum huge page size. This is useful 227a75eb03bSDavid Marchand * if the shm file is in a hugetlbfs that cannot be truncated to the 228a75eb03bSDavid Marchand * shm_size value. */ 229a75eb03bSDavid Marchand static int 230a75eb03bSDavid Marchand ivshmem_server_ftruncate(int fd, unsigned shmsize) 231a75eb03bSDavid Marchand { 232a75eb03bSDavid Marchand int ret; 233a75eb03bSDavid Marchand struct stat mapstat; 234a75eb03bSDavid Marchand 235a75eb03bSDavid Marchand /* align shmsize to next power of 2 */ 236a75eb03bSDavid Marchand shmsize = pow2ceil(shmsize); 237a75eb03bSDavid Marchand 238a75eb03bSDavid Marchand if (fstat(fd, &mapstat) != -1 && mapstat.st_size == shmsize) { 239a75eb03bSDavid Marchand return 0; 240a75eb03bSDavid Marchand } 241a75eb03bSDavid Marchand 242a75eb03bSDavid Marchand while (shmsize <= IVSHMEM_SERVER_MAX_HUGEPAGE_SIZE) { 243a75eb03bSDavid Marchand ret = ftruncate(fd, shmsize); 244a75eb03bSDavid Marchand if (ret == 0) { 245a75eb03bSDavid Marchand return ret; 246a75eb03bSDavid Marchand } 247a75eb03bSDavid Marchand shmsize *= 2; 248a75eb03bSDavid Marchand } 249a75eb03bSDavid Marchand 250a75eb03bSDavid Marchand return -1; 251a75eb03bSDavid Marchand } 252a75eb03bSDavid Marchand 253a75eb03bSDavid Marchand /* Init a new ivshmem server */ 254a75eb03bSDavid Marchand int 255a75eb03bSDavid Marchand ivshmem_server_init(IvshmemServer *server, const char *unix_sock_path, 2563625c739SMarkus Armbruster const char *shm_path, bool use_shm_open, 2573625c739SMarkus Armbruster 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 2783625c739SMarkus Armbruster server->use_shm_open = use_shm_open; 279a75eb03bSDavid Marchand server->shm_size = shm_size; 280a75eb03bSDavid Marchand server->n_vectors = n_vectors; 281a75eb03bSDavid Marchand 282a75eb03bSDavid Marchand QTAILQ_INIT(&server->peer_list); 283a75eb03bSDavid Marchand 284a75eb03bSDavid Marchand return 0; 285a75eb03bSDavid Marchand } 286a75eb03bSDavid Marchand 287a75eb03bSDavid Marchand /* open shm, create and bind to the unix socket */ 288a75eb03bSDavid Marchand int 289a75eb03bSDavid Marchand ivshmem_server_start(IvshmemServer *server) 290a75eb03bSDavid Marchand { 291*7e47061dSDavid CARLIER struct sockaddr_un s_un; 292a75eb03bSDavid Marchand int shm_fd, sock_fd, ret; 293a75eb03bSDavid Marchand 294a75eb03bSDavid Marchand /* open shm file */ 2953625c739SMarkus Armbruster if (server->use_shm_open) { 2961e21feb6SMarc-André Lureau IVSHMEM_SERVER_DEBUG(server, "Using POSIX shared memory: %s\n", 2971e21feb6SMarc-André Lureau server->shm_path); 298a75eb03bSDavid Marchand shm_fd = shm_open(server->shm_path, O_CREAT | O_RDWR, S_IRWXU); 2993625c739SMarkus Armbruster } else { 3003625c739SMarkus Armbruster gchar *filename = g_strdup_printf("%s/ivshmem.XXXXXX", server->shm_path); 3013625c739SMarkus Armbruster IVSHMEM_SERVER_DEBUG(server, "Using file-backed shared memory: %s\n", 3023625c739SMarkus Armbruster server->shm_path); 3033625c739SMarkus Armbruster shm_fd = mkstemp(filename); 3043625c739SMarkus Armbruster unlink(filename); 3053625c739SMarkus Armbruster g_free(filename); 3061e21feb6SMarc-André Lureau } 3071e21feb6SMarc-André Lureau 308a75eb03bSDavid Marchand if (shm_fd < 0) { 309a75eb03bSDavid Marchand fprintf(stderr, "cannot open shm file %s: %s\n", server->shm_path, 310a75eb03bSDavid Marchand strerror(errno)); 311a75eb03bSDavid Marchand return -1; 312a75eb03bSDavid Marchand } 313a75eb03bSDavid Marchand if (ivshmem_server_ftruncate(shm_fd, server->shm_size) < 0) { 314a75eb03bSDavid Marchand fprintf(stderr, "ftruncate(%s) failed: %s\n", server->shm_path, 315a75eb03bSDavid Marchand strerror(errno)); 316a75eb03bSDavid Marchand goto err_close_shm; 317a75eb03bSDavid Marchand } 318a75eb03bSDavid Marchand 319a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "create & bind socket %s\n", 320a75eb03bSDavid Marchand server->unix_sock_path); 321a75eb03bSDavid Marchand 322a75eb03bSDavid Marchand /* create the unix listening socket */ 323a75eb03bSDavid Marchand sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); 324a75eb03bSDavid Marchand if (sock_fd < 0) { 325a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "cannot create socket: %s\n", 326a75eb03bSDavid Marchand strerror(errno)); 327a75eb03bSDavid Marchand goto err_close_shm; 328a75eb03bSDavid Marchand } 329a75eb03bSDavid Marchand 330*7e47061dSDavid CARLIER s_un.sun_family = AF_UNIX; 331*7e47061dSDavid CARLIER ret = snprintf(s_un.sun_path, sizeof(s_un.sun_path), "%s", 332a75eb03bSDavid Marchand server->unix_sock_path); 333*7e47061dSDavid CARLIER if (ret < 0 || ret >= sizeof(s_un.sun_path)) { 334a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "could not copy unix socket path\n"); 335a75eb03bSDavid Marchand goto err_close_sock; 336a75eb03bSDavid Marchand } 337*7e47061dSDavid CARLIER if (bind(sock_fd, (struct sockaddr *)&s_un, sizeof(s_un)) < 0) { 338*7e47061dSDavid CARLIER IVSHMEM_SERVER_DEBUG(server, "cannot connect to %s: %s\n", s_un.sun_path, 339a75eb03bSDavid Marchand strerror(errno)); 340a75eb03bSDavid Marchand goto err_close_sock; 341a75eb03bSDavid Marchand } 342a75eb03bSDavid Marchand 343a75eb03bSDavid Marchand if (listen(sock_fd, IVSHMEM_SERVER_LISTEN_BACKLOG) < 0) { 344a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "listen() failed: %s\n", strerror(errno)); 345a75eb03bSDavid Marchand goto err_close_sock; 346a75eb03bSDavid Marchand } 347a75eb03bSDavid Marchand 348a75eb03bSDavid Marchand server->sock_fd = sock_fd; 349a75eb03bSDavid Marchand server->shm_fd = shm_fd; 350a75eb03bSDavid Marchand 351a75eb03bSDavid Marchand return 0; 352a75eb03bSDavid Marchand 353a75eb03bSDavid Marchand err_close_sock: 354a75eb03bSDavid Marchand close(sock_fd); 355a75eb03bSDavid Marchand err_close_shm: 3560602a616SJan Kiszka if (server->use_shm_open) { 3570602a616SJan Kiszka shm_unlink(server->shm_path); 3580602a616SJan Kiszka } 359a75eb03bSDavid Marchand close(shm_fd); 360a75eb03bSDavid Marchand return -1; 361a75eb03bSDavid Marchand } 362a75eb03bSDavid Marchand 363a75eb03bSDavid Marchand /* close connections to clients, the unix socket and the shm fd */ 364a75eb03bSDavid Marchand void 365a75eb03bSDavid Marchand ivshmem_server_close(IvshmemServer *server) 366a75eb03bSDavid Marchand { 367a75eb03bSDavid Marchand IvshmemServerPeer *peer, *npeer; 368a75eb03bSDavid Marchand 369a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "close server\n"); 370a75eb03bSDavid Marchand 371a75eb03bSDavid Marchand QTAILQ_FOREACH_SAFE(peer, &server->peer_list, next, npeer) { 372a75eb03bSDavid Marchand ivshmem_server_free_peer(server, peer); 373a75eb03bSDavid Marchand } 374a75eb03bSDavid Marchand 375a75eb03bSDavid Marchand unlink(server->unix_sock_path); 3760602a616SJan Kiszka if (server->use_shm_open) { 3770602a616SJan Kiszka shm_unlink(server->shm_path); 3780602a616SJan Kiszka } 379a75eb03bSDavid Marchand close(server->sock_fd); 380a75eb03bSDavid Marchand close(server->shm_fd); 381a75eb03bSDavid Marchand server->sock_fd = -1; 382a75eb03bSDavid Marchand server->shm_fd = -1; 383a75eb03bSDavid Marchand } 384a75eb03bSDavid Marchand 385a75eb03bSDavid Marchand /* get the fd_set according to the unix socket and the peer list */ 386a75eb03bSDavid Marchand void 387a75eb03bSDavid Marchand ivshmem_server_get_fds(const IvshmemServer *server, fd_set *fds, int *maxfd) 388a75eb03bSDavid Marchand { 389a75eb03bSDavid Marchand IvshmemServerPeer *peer; 390a75eb03bSDavid Marchand 391a75eb03bSDavid Marchand if (server->sock_fd == -1) { 392a75eb03bSDavid Marchand return; 393a75eb03bSDavid Marchand } 394a75eb03bSDavid Marchand 395a75eb03bSDavid Marchand FD_SET(server->sock_fd, fds); 396a75eb03bSDavid Marchand if (server->sock_fd >= *maxfd) { 397a75eb03bSDavid Marchand *maxfd = server->sock_fd + 1; 398a75eb03bSDavid Marchand } 399a75eb03bSDavid Marchand 400a75eb03bSDavid Marchand QTAILQ_FOREACH(peer, &server->peer_list, next) { 401a75eb03bSDavid Marchand FD_SET(peer->sock_fd, fds); 402a75eb03bSDavid Marchand if (peer->sock_fd >= *maxfd) { 403a75eb03bSDavid Marchand *maxfd = peer->sock_fd + 1; 404a75eb03bSDavid Marchand } 405a75eb03bSDavid Marchand } 406a75eb03bSDavid Marchand } 407a75eb03bSDavid Marchand 408a75eb03bSDavid Marchand /* process incoming messages on the sockets in fd_set */ 409a75eb03bSDavid Marchand int 410a75eb03bSDavid Marchand ivshmem_server_handle_fds(IvshmemServer *server, fd_set *fds, int maxfd) 411a75eb03bSDavid Marchand { 412a75eb03bSDavid Marchand IvshmemServerPeer *peer, *peer_next; 413a75eb03bSDavid Marchand 414a75eb03bSDavid Marchand if (server->sock_fd < maxfd && FD_ISSET(server->sock_fd, fds) && 415a75eb03bSDavid Marchand ivshmem_server_handle_new_conn(server) < 0 && errno != EINTR) { 416a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "ivshmem_server_handle_new_conn() " 417a75eb03bSDavid Marchand "failed\n"); 418a75eb03bSDavid Marchand return -1; 419a75eb03bSDavid Marchand } 420a75eb03bSDavid Marchand 421a75eb03bSDavid Marchand QTAILQ_FOREACH_SAFE(peer, &server->peer_list, next, peer_next) { 422a75eb03bSDavid Marchand /* any message from a peer socket result in a close() */ 423a75eb03bSDavid Marchand IVSHMEM_SERVER_DEBUG(server, "peer->sock_fd=%d\n", peer->sock_fd); 424a75eb03bSDavid Marchand if (peer->sock_fd < maxfd && FD_ISSET(peer->sock_fd, fds)) { 425a75eb03bSDavid Marchand ivshmem_server_free_peer(server, peer); 426a75eb03bSDavid Marchand } 427a75eb03bSDavid Marchand } 428a75eb03bSDavid Marchand 429a75eb03bSDavid Marchand return 0; 430a75eb03bSDavid Marchand } 431a75eb03bSDavid Marchand 432a75eb03bSDavid Marchand /* lookup peer from its id */ 433a75eb03bSDavid Marchand IvshmemServerPeer * 434f7a199b2SMarc-André Lureau ivshmem_server_search_peer(IvshmemServer *server, int64_t peer_id) 435a75eb03bSDavid Marchand { 436a75eb03bSDavid Marchand IvshmemServerPeer *peer; 437a75eb03bSDavid Marchand 438a75eb03bSDavid Marchand QTAILQ_FOREACH(peer, &server->peer_list, next) { 439a75eb03bSDavid Marchand if (peer->id == peer_id) { 440a75eb03bSDavid Marchand return peer; 441a75eb03bSDavid Marchand } 442a75eb03bSDavid Marchand } 443a75eb03bSDavid Marchand return NULL; 444a75eb03bSDavid Marchand } 445a75eb03bSDavid Marchand 446a75eb03bSDavid Marchand /* dump our info, the list of peers their vectors on stdout */ 447a75eb03bSDavid Marchand void 448a75eb03bSDavid Marchand ivshmem_server_dump(const IvshmemServer *server) 449a75eb03bSDavid Marchand { 450a75eb03bSDavid Marchand const IvshmemServerPeer *peer; 451a75eb03bSDavid Marchand unsigned vector; 452a75eb03bSDavid Marchand 453a75eb03bSDavid Marchand /* dump peers */ 454a75eb03bSDavid Marchand QTAILQ_FOREACH(peer, &server->peer_list, next) { 455f7a199b2SMarc-André Lureau printf("peer_id = %" PRId64 "\n", peer->id); 456a75eb03bSDavid Marchand 457a75eb03bSDavid Marchand for (vector = 0; vector < peer->vectors_count; vector++) { 458a75eb03bSDavid Marchand printf(" vector %d is enabled (fd=%d)\n", vector, 459a75eb03bSDavid Marchand event_notifier_get_fd(&peer->vectors[vector])); 460a75eb03bSDavid Marchand } 461a75eb03bSDavid Marchand } 462a75eb03bSDavid Marchand } 463