1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
14 #define RTE_LOGTYPE_VHOST_FDMAN RTE_LOGTYPE_USER1
16 #define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
19 get_last_valid_idx(struct fdset *pfdset, int last_valid_idx)
23 for (i = last_valid_idx; i >= 0 && pfdset->fd[i].fd == -1; i--)
30 fdset_move(struct fdset *pfdset, int dst, int src)
32 pfdset->fd[dst] = pfdset->fd[src];
33 pfdset->rwfds[dst] = pfdset->rwfds[src];
37 fdset_shrink_nolock(struct fdset *pfdset)
40 int last_valid_idx = get_last_valid_idx(pfdset, pfdset->num - 1);
42 for (i = 0; i < last_valid_idx; i++) {
43 if (pfdset->fd[i].fd != -1)
46 fdset_move(pfdset, i, last_valid_idx);
47 last_valid_idx = get_last_valid_idx(pfdset, last_valid_idx - 1);
49 pfdset->num = last_valid_idx + 1;
53 * Find deleted fd entries and remove them
56 fdset_shrink(struct fdset *pfdset)
58 pthread_mutex_lock(&pfdset->fd_mutex);
59 fdset_shrink_nolock(pfdset);
60 pthread_mutex_unlock(&pfdset->fd_mutex);
64 * Returns the index in the fdset for a given fd.
66 * index for the fd, or -1 if fd isn't in the fdset.
69 fdset_find_fd(struct fdset *pfdset, int fd)
73 for (i = 0; i < pfdset->num && pfdset->fd[i].fd != fd; i++)
76 return i == pfdset->num ? -1 : i;
80 fdset_add_fd(struct fdset *pfdset, int idx, int fd,
81 fd_cb rcb, fd_cb wcb, void *dat)
83 struct fdentry *pfdentry = &pfdset->fd[idx];
84 struct pollfd *pfd = &pfdset->rwfds[idx];
92 pfd->events = rcb ? POLLIN : 0;
93 pfd->events |= wcb ? POLLOUT : 0;
98 fdset_init(struct fdset *pfdset)
105 for (i = 0; i < MAX_FDS; i++) {
106 pfdset->fd[i].fd = -1;
107 pfdset->fd[i].dat = NULL;
113 * Register the fd in the fdset with read/write handler and context.
116 fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
120 if (pfdset == NULL || fd == -1)
123 pthread_mutex_lock(&pfdset->fd_mutex);
124 i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
126 pthread_mutex_lock(&pfdset->fd_pooling_mutex);
127 fdset_shrink_nolock(pfdset);
128 pthread_mutex_unlock(&pfdset->fd_pooling_mutex);
129 i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
131 pthread_mutex_unlock(&pfdset->fd_mutex);
136 fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
137 pthread_mutex_unlock(&pfdset->fd_mutex);
143 * Unregister the fd from the fdset.
144 * Returns context of a given fd or NULL.
147 fdset_del(struct fdset *pfdset, int fd)
152 if (pfdset == NULL || fd == -1)
156 pthread_mutex_lock(&pfdset->fd_mutex);
158 i = fdset_find_fd(pfdset, fd);
159 if (i != -1 && pfdset->fd[i].busy == 0) {
160 /* busy indicates r/wcb is executing! */
161 dat = pfdset->fd[i].dat;
162 pfdset->fd[i].fd = -1;
163 pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
164 pfdset->fd[i].dat = NULL;
167 pthread_mutex_unlock(&pfdset->fd_mutex);
174 * Unregister the fd from the fdset.
176 * If parameters are invalid, return directly -2.
177 * And check whether fd is busy, if yes, return -1.
178 * Otherwise, try to delete the fd from fdset and
182 fdset_try_del(struct fdset *pfdset, int fd)
186 if (pfdset == NULL || fd == -1)
189 pthread_mutex_lock(&pfdset->fd_mutex);
190 i = fdset_find_fd(pfdset, fd);
191 if (i != -1 && pfdset->fd[i].busy) {
192 pthread_mutex_unlock(&pfdset->fd_mutex);
197 pfdset->fd[i].fd = -1;
198 pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
199 pfdset->fd[i].dat = NULL;
202 pthread_mutex_unlock(&pfdset->fd_mutex);
207 * This functions runs in infinite blocking loop until there is no fd in
208 * pfdset. It calls corresponding r/w handler if there is event on the fd.
210 * Before the callback is called, we set the flag to busy status; If other
211 * thread(now rte_vhost_driver_unregister) calls fdset_del concurrently, it
212 * will wait until the flag is reset to zero(which indicates the callback is
213 * finished), then it could free the context after fdset_del.
216 fdset_event_dispatch(void *arg)
220 struct fdentry *pfdentry;
224 int remove1, remove2;
226 struct fdset *pfdset = arg;
235 * When poll is blocked, other threads might unregister
236 * listenfds from and register new listenfds into fdset.
237 * When poll returns, the entries for listenfds in the fdset
238 * might have been updated. It is ok if there is unwanted call
241 pthread_mutex_lock(&pfdset->fd_mutex);
242 numfds = pfdset->num;
243 pthread_mutex_unlock(&pfdset->fd_mutex);
245 pthread_mutex_lock(&pfdset->fd_pooling_mutex);
246 val = poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
247 pthread_mutex_unlock(&pfdset->fd_pooling_mutex);
252 for (i = 0; i < numfds; i++) {
253 pthread_mutex_lock(&pfdset->fd_mutex);
255 pfdentry = &pfdset->fd[i];
257 pfd = &pfdset->rwfds[i];
261 pthread_mutex_unlock(&pfdset->fd_mutex);
266 pthread_mutex_unlock(&pfdset->fd_mutex);
270 remove1 = remove2 = 0;
277 pthread_mutex_unlock(&pfdset->fd_mutex);
279 if (rcb && pfd->revents & (POLLIN | FDPOLLERR))
280 rcb(fd, dat, &remove1);
281 if (wcb && pfd->revents & (POLLOUT | FDPOLLERR))
282 wcb(fd, dat, &remove2);
285 * fdset_del needs to check busy flag.
286 * We don't allow fdset_del to be called in callback
290 * When we are to clean up the fd from fdset,
291 * because the fd is closed in the cb,
292 * the old fd val could be reused by when creates new
293 * listen fd in another thread, we couldn't call
296 if (remove1 || remove2) {
303 fdset_shrink(pfdset);
310 fdset_pipe_read_cb(int readfd, void *dat __rte_unused,
311 int *remove __rte_unused)
314 int r = read(readfd, charbuf, sizeof(charbuf));
316 * Just an optimization, we don't care if read() failed
317 * so ignore explicitly its return value to make the
324 fdset_pipe_uninit(struct fdset *fdset)
326 fdset_del(fdset, fdset->u.readfd);
327 close(fdset->u.readfd);
328 close(fdset->u.writefd);
332 fdset_pipe_init(struct fdset *fdset)
336 if (pipe(fdset->u.pipefd) < 0) {
337 RTE_LOG(ERR, VHOST_FDMAN,
338 "failed to create pipe for vhost fdset\n");
342 ret = fdset_add(fdset, fdset->u.readfd,
343 fdset_pipe_read_cb, NULL, NULL);
346 RTE_LOG(ERR, VHOST_FDMAN,
347 "failed to add pipe readfd %d into vhost server fdset\n",
350 fdset_pipe_uninit(fdset);
358 fdset_pipe_notify(struct fdset *fdset)
360 int r = write(fdset->u.writefd, "1", 1);
362 * Just an optimization, we don't care if write() failed
363 * so ignore explicitly its return value to make the