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