Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20201215' into...
[qemu/ar7.git] / contrib / ivshmem-client / ivshmem-client.h
blobfc45a38060f03ab9d71a519aa8ac06a142c5cb47
1 /*
2 * Copyright 6WIND S.A., 2014
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 */
9 #ifndef IVSHMEM_CLIENT_H
10 #define IVSHMEM_CLIENT_H
12 /**
13 * This file provides helper to implement an ivshmem client. It is used
14 * on the host to ask QEMU to send an interrupt to an ivshmem PCI device in a
15 * guest. QEMU also implements an ivshmem client similar to this one, they both
16 * connect to an ivshmem server.
18 * A standalone ivshmem client based on this file is provided for debug/test
19 * purposes.
22 #include <sys/select.h>
24 #include "qemu/queue.h"
25 #include "hw/misc/ivshmem.h"
27 /**
28 * Maximum number of notification vectors supported by the client
30 #define IVSHMEM_CLIENT_MAX_VECTORS 64
32 /**
33 * Structure storing a peer
35 * Each time a client connects to an ivshmem server, it is advertised to
36 * all connected clients through the unix socket. When our ivshmem
37 * client receives a notification, it creates a IvshmemClientPeer
38 * structure to store the infos of this peer.
40 * This structure is also used to store the information of our own
41 * client in (IvshmemClient)->local.
43 typedef struct IvshmemClientPeer {
44 QTAILQ_ENTRY(IvshmemClientPeer) next; /**< next in list*/
45 int64_t id; /**< the id of the peer */
46 int vectors[IVSHMEM_CLIENT_MAX_VECTORS]; /**< one fd per vector */
47 unsigned vectors_count; /**< number of vectors */
48 } IvshmemClientPeer;
50 typedef struct IvshmemClient IvshmemClient;
52 /**
53 * Typedef of callback function used when our IvshmemClient receives a
54 * notification from a peer.
56 typedef void (*IvshmemClientNotifCb)(
57 const IvshmemClient *client,
58 const IvshmemClientPeer *peer,
59 unsigned vect, void *arg);
61 /**
62 * Structure describing an ivshmem client
64 * This structure stores all information related to our client: the name
65 * of the server unix socket, the list of peers advertised by the
66 * server, our own client information, and a pointer the notification
67 * callback function used when we receive a notification from a peer.
69 struct IvshmemClient {
70 char unix_sock_path[PATH_MAX]; /**< path to unix sock */
71 int sock_fd; /**< unix sock filedesc */
72 int shm_fd; /**< shm file descriptor */
74 QTAILQ_HEAD(, IvshmemClientPeer) peer_list; /**< list of peers */
75 IvshmemClientPeer local; /**< our own infos */
77 IvshmemClientNotifCb notif_cb; /**< notification callback */
78 void *notif_arg; /**< notification argument */
80 bool verbose; /**< true to enable debug */
83 /**
84 * Initialize an ivshmem client
86 * @client: A pointer to an uninitialized IvshmemClient structure
87 * @unix_sock_path: The pointer to the unix socket file name
88 * @notif_cb: If not NULL, the pointer to the function to be called when
89 * our IvshmemClient receives a notification from a peer
90 * @notif_arg: Opaque pointer given as-is to the notification callback
91 * function
92 * @verbose: True to enable debug
94 * Returns: 0 on success, or a negative value on error
96 int ivshmem_client_init(IvshmemClient *client, const char *unix_sock_path,
97 IvshmemClientNotifCb notif_cb, void *notif_arg,
98 bool verbose);
101 * Connect to the server
103 * Connect to the server unix socket, and read the first initial
104 * messages sent by the server, giving the ID of the client and the file
105 * descriptor of the shared memory.
107 * @client: The ivshmem client
109 * Returns: 0 on success, or a negative value on error
111 int ivshmem_client_connect(IvshmemClient *client);
114 * Close connection to the server and free all peer structures
116 * @client: The ivshmem client
118 void ivshmem_client_close(IvshmemClient *client);
121 * Fill a fd_set with file descriptors to be monitored
123 * This function will fill a fd_set with all file descriptors
124 * that must be polled (unix server socket and peers eventfd). The
125 * function will not initialize the fd_set, it is up to the caller
126 * to do this.
128 * @client: The ivshmem client
129 * @fds: The fd_set to be updated
130 * @maxfd: Must be set to the max file descriptor + 1 in fd_set. This value is
131 * updated if this function adds a greater fd in fd_set.
133 void ivshmem_client_get_fds(const IvshmemClient *client, fd_set *fds,
134 int *maxfd);
137 * Read and handle new messages
139 * Given a fd_set filled by select(), handle incoming messages from
140 * server or peers.
142 * @client: The ivshmem client
143 * @fds: The fd_set containing the file descriptors to be checked. Note
144 * that file descriptors that are not related to our client are
145 * ignored.
146 * @maxfd: The maximum fd in fd_set, plus one.
148 * Returns: 0 on success, or a negative value on error
150 int ivshmem_client_handle_fds(IvshmemClient *client, fd_set *fds, int maxfd);
153 * Send a notification to a vector of a peer
155 * @client: The ivshmem client
156 * @peer: The peer to be notified
157 * @vector: The number of the vector
159 * Returns: 0 on success, or a negative value on error
161 int ivshmem_client_notify(const IvshmemClient *client,
162 const IvshmemClientPeer *peer, unsigned vector);
165 * Send a notification to all vectors of a peer
167 * @client: The ivshmem client
168 * @peer: The peer to be notified
170 * Returns: 0 on success, or a negative value on error (at least one
171 * notification failed)
173 int ivshmem_client_notify_all_vects(const IvshmemClient *client,
174 const IvshmemClientPeer *peer);
177 * Broadcast a notification to all vectors of all peers
179 * @client: The ivshmem client
181 * Returns: 0 on success, or a negative value on error (at least one
182 * notification failed)
184 int ivshmem_client_notify_broadcast(const IvshmemClient *client);
187 * Search a peer from its identifier
189 * Return the peer structure from its peer_id. If the given peer_id is
190 * the local id, the function returns the local peer structure.
192 * @client: The ivshmem client
193 * @peer_id: The identifier of the peer structure
195 * Returns: The peer structure, or NULL if not found
197 IvshmemClientPeer *
198 ivshmem_client_search_peer(IvshmemClient *client, int64_t peer_id);
201 * Dump information of this ivshmem client on stdout
203 * Dump the id and the vectors of the given ivshmem client and the list
204 * of its peers and their vectors on stdout.
206 * @client: The ivshmem client
208 void ivshmem_client_dump(const IvshmemClient *client);
210 #endif /* IVSHMEM_CLIENT_H */