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 9 #include <sys/types.h> 10 #include <sys/socket.h> 11 #include <sys/un.h> 12 13 #include "qemu-common.h" 14 #include "qemu/queue.h" 15 16 #include "ivshmem-client.h" 17 18 /* log a message on stdout if verbose=1 */ 19 #define IVSHMEM_CLIENT_DEBUG(client, fmt, ...) do { \ 20 if ((client)->verbose) { \ 21 printf(fmt, ## __VA_ARGS__); \ 22 } \ 23 } while (0) 24 25 /* read message from the unix socket */ 26 static int 27 ivshmem_client_read_one_msg(IvshmemClient *client, long *index, int *fd) 28 { 29 int ret; 30 struct msghdr msg; 31 struct iovec iov[1]; 32 union { 33 struct cmsghdr cmsg; 34 char control[CMSG_SPACE(sizeof(int))]; 35 } msg_control; 36 struct cmsghdr *cmsg; 37 38 iov[0].iov_base = index; 39 iov[0].iov_len = sizeof(*index); 40 41 memset(&msg, 0, sizeof(msg)); 42 msg.msg_iov = iov; 43 msg.msg_iovlen = 1; 44 msg.msg_control = &msg_control; 45 msg.msg_controllen = sizeof(msg_control); 46 47 ret = recvmsg(client->sock_fd, &msg, 0); 48 if (ret < 0) { 49 IVSHMEM_CLIENT_DEBUG(client, "cannot read message: %s\n", 50 strerror(errno)); 51 return -1; 52 } 53 if (ret == 0) { 54 IVSHMEM_CLIENT_DEBUG(client, "lost connection to server\n"); 55 return -1; 56 } 57 58 *fd = -1; 59 60 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 61 62 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) || 63 cmsg->cmsg_level != SOL_SOCKET || 64 cmsg->cmsg_type != SCM_RIGHTS) { 65 continue; 66 } 67 68 memcpy(fd, CMSG_DATA(cmsg), sizeof(*fd)); 69 } 70 71 return 0; 72 } 73 74 /* free a peer when the server advertises a disconnection or when the 75 * client is freed */ 76 static void 77 ivshmem_client_free_peer(IvshmemClient *client, IvshmemClientPeer *peer) 78 { 79 unsigned vector; 80 81 QTAILQ_REMOVE(&client->peer_list, peer, next); 82 for (vector = 0; vector < peer->vectors_count; vector++) { 83 close(peer->vectors[vector]); 84 } 85 86 g_free(peer); 87 } 88 89 /* handle message coming from server (new peer, new vectors) */ 90 static int 91 ivshmem_client_handle_server_msg(IvshmemClient *client) 92 { 93 IvshmemClientPeer *peer; 94 long peer_id; 95 int ret, fd; 96 97 ret = ivshmem_client_read_one_msg(client, &peer_id, &fd); 98 if (ret < 0) { 99 return -1; 100 } 101 102 /* can return a peer or the local client */ 103 peer = ivshmem_client_search_peer(client, peer_id); 104 105 /* delete peer */ 106 if (fd == -1) { 107 108 if (peer == NULL || peer == &client->local) { 109 IVSHMEM_CLIENT_DEBUG(client, "receive delete for invalid " 110 "peer %ld\n", peer_id); 111 return -1; 112 } 113 114 IVSHMEM_CLIENT_DEBUG(client, "delete peer id = %ld\n", peer_id); 115 ivshmem_client_free_peer(client, peer); 116 return 0; 117 } 118 119 /* new peer */ 120 if (peer == NULL) { 121 peer = g_malloc0(sizeof(*peer)); 122 peer->id = peer_id; 123 peer->vectors_count = 0; 124 QTAILQ_INSERT_TAIL(&client->peer_list, peer, next); 125 IVSHMEM_CLIENT_DEBUG(client, "new peer id = %ld\n", peer_id); 126 } 127 128 /* new vector */ 129 IVSHMEM_CLIENT_DEBUG(client, " new vector %d (fd=%d) for peer id %ld\n", 130 peer->vectors_count, fd, peer->id); 131 if (peer->vectors_count >= G_N_ELEMENTS(peer->vectors)) { 132 IVSHMEM_CLIENT_DEBUG(client, "Too many vectors received, failing"); 133 return -1; 134 } 135 136 peer->vectors[peer->vectors_count] = fd; 137 peer->vectors_count++; 138 139 return 0; 140 } 141 142 /* init a new ivshmem client */ 143 int 144 ivshmem_client_init(IvshmemClient *client, const char *unix_sock_path, 145 IvshmemClientNotifCb notif_cb, void *notif_arg, 146 bool verbose) 147 { 148 int ret; 149 unsigned i; 150 151 memset(client, 0, sizeof(*client)); 152 153 ret = snprintf(client->unix_sock_path, sizeof(client->unix_sock_path), 154 "%s", unix_sock_path); 155 156 if (ret < 0 || ret >= sizeof(client->unix_sock_path)) { 157 IVSHMEM_CLIENT_DEBUG(client, "could not copy unix socket path\n"); 158 return -1; 159 } 160 161 for (i = 0; i < IVSHMEM_CLIENT_MAX_VECTORS; i++) { 162 client->local.vectors[i] = -1; 163 } 164 165 QTAILQ_INIT(&client->peer_list); 166 client->local.id = -1; 167 168 client->notif_cb = notif_cb; 169 client->notif_arg = notif_arg; 170 client->verbose = verbose; 171 client->shm_fd = -1; 172 client->sock_fd = -1; 173 174 return 0; 175 } 176 177 /* create and connect to the unix socket */ 178 int 179 ivshmem_client_connect(IvshmemClient *client) 180 { 181 struct sockaddr_un sun; 182 int fd, ret; 183 long tmp; 184 185 IVSHMEM_CLIENT_DEBUG(client, "connect to client %s\n", 186 client->unix_sock_path); 187 188 client->sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); 189 if (client->sock_fd < 0) { 190 IVSHMEM_CLIENT_DEBUG(client, "cannot create socket: %s\n", 191 strerror(errno)); 192 return -1; 193 } 194 195 sun.sun_family = AF_UNIX; 196 ret = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", 197 client->unix_sock_path); 198 if (ret < 0 || ret >= sizeof(sun.sun_path)) { 199 IVSHMEM_CLIENT_DEBUG(client, "could not copy unix socket path\n"); 200 goto err_close; 201 } 202 203 if (connect(client->sock_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) { 204 IVSHMEM_CLIENT_DEBUG(client, "cannot connect to %s: %s\n", sun.sun_path, 205 strerror(errno)); 206 goto err_close; 207 } 208 209 /* first, we expect our index + a fd == -1 */ 210 if (ivshmem_client_read_one_msg(client, &client->local.id, &fd) < 0 || 211 client->local.id < 0 || fd != -1) { 212 IVSHMEM_CLIENT_DEBUG(client, "cannot read from server\n"); 213 goto err_close; 214 } 215 IVSHMEM_CLIENT_DEBUG(client, "our_id=%ld\n", client->local.id); 216 217 /* now, we expect shared mem fd + a -1 index, note that shm fd 218 * is not used */ 219 if (ivshmem_client_read_one_msg(client, &tmp, &fd) < 0 || 220 tmp != -1 || fd < 0) { 221 if (fd >= 0) { 222 close(fd); 223 } 224 IVSHMEM_CLIENT_DEBUG(client, "cannot read from server (2)\n"); 225 goto err_close; 226 } 227 client->shm_fd = fd; 228 IVSHMEM_CLIENT_DEBUG(client, "shm_fd=%d\n", fd); 229 230 return 0; 231 232 err_close: 233 close(client->sock_fd); 234 client->sock_fd = -1; 235 return -1; 236 } 237 238 /* close connection to the server, and free all peer structures */ 239 void 240 ivshmem_client_close(IvshmemClient *client) 241 { 242 IvshmemClientPeer *peer; 243 unsigned i; 244 245 IVSHMEM_CLIENT_DEBUG(client, "close client\n"); 246 247 while ((peer = QTAILQ_FIRST(&client->peer_list)) != NULL) { 248 ivshmem_client_free_peer(client, peer); 249 } 250 251 close(client->shm_fd); 252 client->shm_fd = -1; 253 close(client->sock_fd); 254 client->sock_fd = -1; 255 client->local.id = -1; 256 for (i = 0; i < IVSHMEM_CLIENT_MAX_VECTORS; i++) { 257 close(client->local.vectors[i]); 258 client->local.vectors[i] = -1; 259 } 260 client->local.vectors_count = 0; 261 } 262 263 /* get the fd_set according to the unix socket and peer list */ 264 void 265 ivshmem_client_get_fds(const IvshmemClient *client, fd_set *fds, int *maxfd) 266 { 267 int fd; 268 unsigned vector; 269 270 FD_SET(client->sock_fd, fds); 271 if (client->sock_fd >= *maxfd) { 272 *maxfd = client->sock_fd + 1; 273 } 274 275 for (vector = 0; vector < client->local.vectors_count; vector++) { 276 fd = client->local.vectors[vector]; 277 FD_SET(fd, fds); 278 if (fd >= *maxfd) { 279 *maxfd = fd + 1; 280 } 281 } 282 } 283 284 /* handle events from eventfd: just print a message on notification */ 285 static int 286 ivshmem_client_handle_event(IvshmemClient *client, const fd_set *cur, int maxfd) 287 { 288 IvshmemClientPeer *peer; 289 uint64_t kick; 290 unsigned i; 291 int ret; 292 293 peer = &client->local; 294 295 for (i = 0; i < peer->vectors_count; i++) { 296 if (peer->vectors[i] >= maxfd || !FD_ISSET(peer->vectors[i], cur)) { 297 continue; 298 } 299 300 ret = read(peer->vectors[i], &kick, sizeof(kick)); 301 if (ret < 0) { 302 return ret; 303 } 304 if (ret != sizeof(kick)) { 305 IVSHMEM_CLIENT_DEBUG(client, "invalid read size = %d\n", ret); 306 errno = EINVAL; 307 return -1; 308 } 309 IVSHMEM_CLIENT_DEBUG(client, "received event on fd %d vector %d: %" 310 PRIu64 "\n", peer->vectors[i], i, kick); 311 if (client->notif_cb != NULL) { 312 client->notif_cb(client, peer, i, client->notif_arg); 313 } 314 } 315 316 return 0; 317 } 318 319 /* read and handle new messages on the given fd_set */ 320 int 321 ivshmem_client_handle_fds(IvshmemClient *client, fd_set *fds, int maxfd) 322 { 323 if (client->sock_fd < maxfd && FD_ISSET(client->sock_fd, fds) && 324 ivshmem_client_handle_server_msg(client) < 0 && errno != EINTR) { 325 IVSHMEM_CLIENT_DEBUG(client, "ivshmem_client_handle_server_msg() " 326 "failed\n"); 327 return -1; 328 } else if (ivshmem_client_handle_event(client, fds, maxfd) < 0 && 329 errno != EINTR) { 330 IVSHMEM_CLIENT_DEBUG(client, "ivshmem_client_handle_event() failed\n"); 331 return -1; 332 } 333 334 return 0; 335 } 336 337 /* send a notification on a vector of a peer */ 338 int 339 ivshmem_client_notify(const IvshmemClient *client, 340 const IvshmemClientPeer *peer, unsigned vector) 341 { 342 uint64_t kick; 343 int fd; 344 345 if (vector >= peer->vectors_count) { 346 IVSHMEM_CLIENT_DEBUG(client, "invalid vector %u on peer %ld\n", 347 vector, peer->id); 348 return -1; 349 } 350 fd = peer->vectors[vector]; 351 IVSHMEM_CLIENT_DEBUG(client, "notify peer %ld on vector %d, fd %d\n", 352 peer->id, vector, fd); 353 354 kick = 1; 355 if (write(fd, &kick, sizeof(kick)) != sizeof(kick)) { 356 fprintf(stderr, "could not write to %d: %s\n", peer->vectors[vector], 357 strerror(errno)); 358 return -1; 359 } 360 return 0; 361 } 362 363 /* send a notification to all vectors of a peer */ 364 int 365 ivshmem_client_notify_all_vects(const IvshmemClient *client, 366 const IvshmemClientPeer *peer) 367 { 368 unsigned vector; 369 int ret = 0; 370 371 for (vector = 0; vector < peer->vectors_count; vector++) { 372 if (ivshmem_client_notify(client, peer, vector) < 0) { 373 ret = -1; 374 } 375 } 376 377 return ret; 378 } 379 380 /* send a notification to all peers */ 381 int 382 ivshmem_client_notify_broadcast(const IvshmemClient *client) 383 { 384 IvshmemClientPeer *peer; 385 int ret = 0; 386 387 QTAILQ_FOREACH(peer, &client->peer_list, next) { 388 if (ivshmem_client_notify_all_vects(client, peer) < 0) { 389 ret = -1; 390 } 391 } 392 393 return ret; 394 } 395 396 /* lookup peer from its id */ 397 IvshmemClientPeer * 398 ivshmem_client_search_peer(IvshmemClient *client, long peer_id) 399 { 400 IvshmemClientPeer *peer; 401 402 if (peer_id == client->local.id) { 403 return &client->local; 404 } 405 406 QTAILQ_FOREACH(peer, &client->peer_list, next) { 407 if (peer->id == peer_id) { 408 return peer; 409 } 410 } 411 return NULL; 412 } 413 414 /* dump our info, the list of peers their vectors on stdout */ 415 void 416 ivshmem_client_dump(const IvshmemClient *client) 417 { 418 const IvshmemClientPeer *peer; 419 unsigned vector; 420 421 /* dump local infos */ 422 peer = &client->local; 423 printf("our_id = %ld\n", peer->id); 424 for (vector = 0; vector < peer->vectors_count; vector++) { 425 printf(" vector %d is enabled (fd=%d)\n", vector, 426 peer->vectors[vector]); 427 } 428 429 /* dump peers */ 430 QTAILQ_FOREACH(peer, &client->peer_list, next) { 431 printf("peer_id = %ld\n", peer->id); 432 433 for (vector = 0; vector < peer->vectors_count; vector++) { 434 printf(" vector %d is enabled (fd=%d)\n", vector, 435 peer->vectors[vector]); 436 } 437 } 438 } 439