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 peer->vectors[peer->vectors_count] = fd; 132 peer->vectors_count++; 133 134 return 0; 135 } 136 137 /* init a new ivshmem client */ 138 int 139 ivshmem_client_init(IvshmemClient *client, const char *unix_sock_path, 140 IvshmemClientNotifCb notif_cb, void *notif_arg, 141 bool verbose) 142 { 143 int ret; 144 unsigned i; 145 146 memset(client, 0, sizeof(*client)); 147 148 ret = snprintf(client->unix_sock_path, sizeof(client->unix_sock_path), 149 "%s", unix_sock_path); 150 151 if (ret < 0 || ret >= sizeof(client->unix_sock_path)) { 152 IVSHMEM_CLIENT_DEBUG(client, "could not copy unix socket path\n"); 153 return -1; 154 } 155 156 for (i = 0; i < IVSHMEM_CLIENT_MAX_VECTORS; i++) { 157 client->local.vectors[i] = -1; 158 } 159 160 QTAILQ_INIT(&client->peer_list); 161 client->local.id = -1; 162 163 client->notif_cb = notif_cb; 164 client->notif_arg = notif_arg; 165 client->verbose = verbose; 166 client->shm_fd = -1; 167 client->sock_fd = -1; 168 169 return 0; 170 } 171 172 /* create and connect to the unix socket */ 173 int 174 ivshmem_client_connect(IvshmemClient *client) 175 { 176 struct sockaddr_un sun; 177 int fd, ret; 178 long tmp; 179 180 IVSHMEM_CLIENT_DEBUG(client, "connect to client %s\n", 181 client->unix_sock_path); 182 183 client->sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); 184 if (client->sock_fd < 0) { 185 IVSHMEM_CLIENT_DEBUG(client, "cannot create socket: %s\n", 186 strerror(errno)); 187 return -1; 188 } 189 190 sun.sun_family = AF_UNIX; 191 ret = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", 192 client->unix_sock_path); 193 if (ret < 0 || ret >= sizeof(sun.sun_path)) { 194 IVSHMEM_CLIENT_DEBUG(client, "could not copy unix socket path\n"); 195 goto err_close; 196 } 197 198 if (connect(client->sock_fd, (struct sockaddr *)&sun, sizeof(sun)) < 0) { 199 IVSHMEM_CLIENT_DEBUG(client, "cannot connect to %s: %s\n", sun.sun_path, 200 strerror(errno)); 201 goto err_close; 202 } 203 204 /* first, we expect our index + a fd == -1 */ 205 if (ivshmem_client_read_one_msg(client, &client->local.id, &fd) < 0 || 206 client->local.id < 0 || fd != -1) { 207 IVSHMEM_CLIENT_DEBUG(client, "cannot read from server\n"); 208 goto err_close; 209 } 210 IVSHMEM_CLIENT_DEBUG(client, "our_id=%ld\n", client->local.id); 211 212 /* now, we expect shared mem fd + a -1 index, note that shm fd 213 * is not used */ 214 if (ivshmem_client_read_one_msg(client, &tmp, &fd) < 0 || 215 tmp != -1 || fd < 0) { 216 if (fd >= 0) { 217 close(fd); 218 } 219 IVSHMEM_CLIENT_DEBUG(client, "cannot read from server (2)\n"); 220 goto err_close; 221 } 222 client->shm_fd = fd; 223 IVSHMEM_CLIENT_DEBUG(client, "shm_fd=%d\n", fd); 224 225 return 0; 226 227 err_close: 228 close(client->sock_fd); 229 client->sock_fd = -1; 230 return -1; 231 } 232 233 /* close connection to the server, and free all peer structures */ 234 void 235 ivshmem_client_close(IvshmemClient *client) 236 { 237 IvshmemClientPeer *peer; 238 unsigned i; 239 240 IVSHMEM_CLIENT_DEBUG(client, "close client\n"); 241 242 while ((peer = QTAILQ_FIRST(&client->peer_list)) != NULL) { 243 ivshmem_client_free_peer(client, peer); 244 } 245 246 close(client->shm_fd); 247 client->shm_fd = -1; 248 close(client->sock_fd); 249 client->sock_fd = -1; 250 client->local.id = -1; 251 for (i = 0; i < IVSHMEM_CLIENT_MAX_VECTORS; i++) { 252 close(client->local.vectors[i]); 253 client->local.vectors[i] = -1; 254 } 255 client->local.vectors_count = 0; 256 } 257 258 /* get the fd_set according to the unix socket and peer list */ 259 void 260 ivshmem_client_get_fds(const IvshmemClient *client, fd_set *fds, int *maxfd) 261 { 262 int fd; 263 unsigned vector; 264 265 FD_SET(client->sock_fd, fds); 266 if (client->sock_fd >= *maxfd) { 267 *maxfd = client->sock_fd + 1; 268 } 269 270 for (vector = 0; vector < client->local.vectors_count; vector++) { 271 fd = client->local.vectors[vector]; 272 FD_SET(fd, fds); 273 if (fd >= *maxfd) { 274 *maxfd = fd + 1; 275 } 276 } 277 } 278 279 /* handle events from eventfd: just print a message on notification */ 280 static int 281 ivshmem_client_handle_event(IvshmemClient *client, const fd_set *cur, int maxfd) 282 { 283 IvshmemClientPeer *peer; 284 uint64_t kick; 285 unsigned i; 286 int ret; 287 288 peer = &client->local; 289 290 for (i = 0; i < peer->vectors_count; i++) { 291 if (peer->vectors[i] >= maxfd || !FD_ISSET(peer->vectors[i], cur)) { 292 continue; 293 } 294 295 ret = read(peer->vectors[i], &kick, sizeof(kick)); 296 if (ret < 0) { 297 return ret; 298 } 299 if (ret != sizeof(kick)) { 300 IVSHMEM_CLIENT_DEBUG(client, "invalid read size = %d\n", ret); 301 errno = EINVAL; 302 return -1; 303 } 304 IVSHMEM_CLIENT_DEBUG(client, "received event on fd %d vector %d: %" 305 PRIu64 "\n", peer->vectors[i], i, kick); 306 if (client->notif_cb != NULL) { 307 client->notif_cb(client, peer, i, client->notif_arg); 308 } 309 } 310 311 return 0; 312 } 313 314 /* read and handle new messages on the given fd_set */ 315 int 316 ivshmem_client_handle_fds(IvshmemClient *client, fd_set *fds, int maxfd) 317 { 318 if (client->sock_fd < maxfd && FD_ISSET(client->sock_fd, fds) && 319 ivshmem_client_handle_server_msg(client) < 0 && errno != EINTR) { 320 IVSHMEM_CLIENT_DEBUG(client, "ivshmem_client_handle_server_msg() " 321 "failed\n"); 322 return -1; 323 } else if (ivshmem_client_handle_event(client, fds, maxfd) < 0 && 324 errno != EINTR) { 325 IVSHMEM_CLIENT_DEBUG(client, "ivshmem_client_handle_event() failed\n"); 326 return -1; 327 } 328 329 return 0; 330 } 331 332 /* send a notification on a vector of a peer */ 333 int 334 ivshmem_client_notify(const IvshmemClient *client, 335 const IvshmemClientPeer *peer, unsigned vector) 336 { 337 uint64_t kick; 338 int fd; 339 340 if (vector >= peer->vectors_count) { 341 IVSHMEM_CLIENT_DEBUG(client, "invalid vector %u on peer %ld\n", 342 vector, peer->id); 343 return -1; 344 } 345 fd = peer->vectors[vector]; 346 IVSHMEM_CLIENT_DEBUG(client, "notify peer %ld on vector %d, fd %d\n", 347 peer->id, vector, fd); 348 349 kick = 1; 350 if (write(fd, &kick, sizeof(kick)) != sizeof(kick)) { 351 fprintf(stderr, "could not write to %d: %s\n", peer->vectors[vector], 352 strerror(errno)); 353 return -1; 354 } 355 return 0; 356 } 357 358 /* send a notification to all vectors of a peer */ 359 int 360 ivshmem_client_notify_all_vects(const IvshmemClient *client, 361 const IvshmemClientPeer *peer) 362 { 363 unsigned vector; 364 int ret = 0; 365 366 for (vector = 0; vector < peer->vectors_count; vector++) { 367 if (ivshmem_client_notify(client, peer, vector) < 0) { 368 ret = -1; 369 } 370 } 371 372 return ret; 373 } 374 375 /* send a notification to all peers */ 376 int 377 ivshmem_client_notify_broadcast(const IvshmemClient *client) 378 { 379 IvshmemClientPeer *peer; 380 int ret = 0; 381 382 QTAILQ_FOREACH(peer, &client->peer_list, next) { 383 if (ivshmem_client_notify_all_vects(client, peer) < 0) { 384 ret = -1; 385 } 386 } 387 388 return ret; 389 } 390 391 /* lookup peer from its id */ 392 IvshmemClientPeer * 393 ivshmem_client_search_peer(IvshmemClient *client, long peer_id) 394 { 395 IvshmemClientPeer *peer; 396 397 if (peer_id == client->local.id) { 398 return &client->local; 399 } 400 401 QTAILQ_FOREACH(peer, &client->peer_list, next) { 402 if (peer->id == peer_id) { 403 return peer; 404 } 405 } 406 return NULL; 407 } 408 409 /* dump our info, the list of peers their vectors on stdout */ 410 void 411 ivshmem_client_dump(const IvshmemClient *client) 412 { 413 const IvshmemClientPeer *peer; 414 unsigned vector; 415 416 /* dump local infos */ 417 peer = &client->local; 418 printf("our_id = %ld\n", peer->id); 419 for (vector = 0; vector < peer->vectors_count; vector++) { 420 printf(" vector %d is enabled (fd=%d)\n", vector, 421 peer->vectors[vector]); 422 } 423 424 /* dump peers */ 425 QTAILQ_FOREACH(peer, &client->peer_list, next) { 426 printf("peer_id = %ld\n", peer->id); 427 428 for (vector = 0; vector < peer->vectors_count; vector++) { 429 printf(" vector %d is enabled (fd=%d)\n", vector, 430 peer->vectors[vector]); 431 } 432 } 433 } 434