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 #ifndef IVSHMEM_SERVER_H
10 #define IVSHMEM_SERVER_H
11 
12 /**
13  * The ivshmem server is a daemon that creates a unix socket in listen
14  * mode. The ivshmem clients (qemu or ivshmem-client) connect to this
15  * unix socket. For each client, the server will create some eventfd
16  * (see EVENTFD(2)), one per vector. These fd are transmitted to all
17  * clients using the SCM_RIGHTS cmsg message. Therefore, each client is
18  * able to send a notification to another client without being
19  * "profixied" by the server.
20  *
21  * We use this mechanism to send interruptions between guests.
22  * qemu is able to transform an event on a eventfd into a PCI MSI-x
23  * interruption in the guest.
24  *
25  * The ivshmem server is also able to share the file descriptor
26  * associated to the ivshmem shared memory.
27  */
28 
29 #include <sys/select.h>
30 
31 #include "qemu/event_notifier.h"
32 #include "qemu/queue.h"
33 #include "hw/misc/ivshmem.h"
34 
35 /**
36  * Maximum number of notification vectors supported by the server
37  */
38 #define IVSHMEM_SERVER_MAX_VECTORS 64
39 
40 /**
41  * Structure storing a peer
42  *
43  * Each time a client connects to an ivshmem server, a new
44  * IvshmemServerPeer structure is created. This peer and all its
45  * vectors are advertised to all connected clients through the connected
46  * unix sockets.
47  */
48 typedef struct IvshmemServerPeer {
49     QTAILQ_ENTRY(IvshmemServerPeer) next;    /**< next in list*/
50     int sock_fd;                             /**< connected unix sock */
51     int64_t id;                              /**< the id of the peer */
52     EventNotifier vectors[IVSHMEM_SERVER_MAX_VECTORS]; /**< one per vector */
53     unsigned vectors_count;                  /**< number of vectors */
54 } IvshmemServerPeer;
55 QTAILQ_HEAD(IvshmemServerPeerList, IvshmemServerPeer);
56 
57 typedef struct IvshmemServerPeerList IvshmemServerPeerList;
58 
59 /**
60  * Structure describing an ivshmem server
61  *
62  * This structure stores all information related to our server: the name
63  * of the server unix socket and the list of connected peers.
64  */
65 typedef struct IvshmemServer {
66     char unix_sock_path[PATH_MAX];   /**< path to unix socket */
67     int sock_fd;                     /**< unix sock file descriptor */
68     char shm_path[PATH_MAX];         /**< path to shm */
69     bool use_shm_open;
70     size_t shm_size;                 /**< size of shm */
71     int shm_fd;                      /**< shm file descriptor */
72     unsigned n_vectors;              /**< number of vectors */
73     uint16_t cur_id;                 /**< id to be given to next client */
74     bool verbose;                    /**< true in verbose mode */
75     IvshmemServerPeerList peer_list; /**< list of peers */
76 } IvshmemServer;
77 
78 /**
79  * Initialize an ivshmem server
80  *
81  * @server:         A pointer to an uninitialized IvshmemServer structure
82  * @unix_sock_path: The pointer to the unix socket file name
83  * @shm_path:       Path to the shared memory. The path corresponds to a POSIX
84  *                  shm name or a hugetlbfs mount point.
85  * @shm_size:       Size of shared memory
86  * @n_vectors:      Number of interrupt vectors per client
87  * @verbose:        True to enable verbose mode
88  *
89  * Returns:         0 on success, or a negative value on error
90  */
91 int
92 ivshmem_server_init(IvshmemServer *server, const char *unix_sock_path,
93                     const char *shm_path, bool use_shm_open,
94                     size_t shm_size, unsigned n_vectors,
95                     bool verbose);
96 
97 /**
98  * Open the shm, then create and bind to the unix socket
99  *
100  * @server: The pointer to the initialized IvshmemServer structure
101  *
102  * Returns: 0 on success, or a negative value on error
103  */
104 int ivshmem_server_start(IvshmemServer *server);
105 
106 /**
107  * Close the server
108  *
109  * Close connections to all clients, close the unix socket and the
110  * shared memory file descriptor. The structure remains initialized, so
111  * it is possible to call ivshmem_server_start() again after a call to
112  * ivshmem_server_close().
113  *
114  * @server: The ivshmem server
115  */
116 void ivshmem_server_close(IvshmemServer *server);
117 
118 /**
119  * Fill a fd_set with file descriptors to be monitored
120  *
121  * This function will fill a fd_set with all file descriptors that must
122  * be polled (unix server socket and peers unix socket). The function
123  * will not initialize the fd_set, it is up to the caller to do it.
124  *
125  * @server: The ivshmem server
126  * @fds:    The fd_set to be updated
127  * @maxfd:  Must be set to the max file descriptor + 1 in fd_set. This value is
128  *          updated if this function adds a greater fd in fd_set.
129  */
130 void
131 ivshmem_server_get_fds(const IvshmemServer *server, fd_set *fds, int *maxfd);
132 
133 /**
134  * Read and handle new messages
135  *
136  * Given a fd_set (for instance filled by a call to select()), handle
137  * incoming messages from peers.
138  *
139  * @server: The ivshmem server
140  * @fds:    The fd_set containing the file descriptors to be checked. Note that
141  *          file descriptors that are not related to our server are ignored.
142  * @maxfd:  The maximum fd in fd_set, plus one.
143  *
144  * Returns: 0 on success, or a negative value on error
145  */
146 int ivshmem_server_handle_fds(IvshmemServer *server, fd_set *fds, int maxfd);
147 
148 /**
149  * Search a peer from its identifier
150  *
151  * @server:  The ivshmem server
152  * @peer_id: The identifier of the peer structure
153  *
154  * Returns:  The peer structure, or NULL if not found
155  */
156 IvshmemServerPeer *
157 ivshmem_server_search_peer(IvshmemServer *server, int64_t peer_id);
158 
159 /**
160  * Dump information of this ivshmem server and its peers on stdout
161  *
162  * @server: The ivshmem server
163  */
164 void ivshmem_server_dump(const IvshmemServer *server);
165 
166 #endif /* IVSHMEM_SERVER_H */
167