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