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