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