1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <sys/socket.h>
10 #include <sys/types.h>
14 #include <rte_common.h>
19 #define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
22 get_last_valid_idx(struct fdset *pfdset, int last_valid_idx)
26 for (i = last_valid_idx; i >= 0 && pfdset->fd[i].fd == -1; i--)
33 fdset_move(struct fdset *pfdset, int dst, int src)
35 pfdset->fd[dst] = pfdset->fd[src];
36 pfdset->rwfds[dst] = pfdset->rwfds[src];
40 fdset_shrink_nolock(struct fdset *pfdset)
43 int last_valid_idx = get_last_valid_idx(pfdset, pfdset->num - 1);
45 for (i = 0; i < last_valid_idx; i++) {
46 if (pfdset->fd[i].fd != -1)
49 fdset_move(pfdset, i, last_valid_idx);
50 last_valid_idx = get_last_valid_idx(pfdset, last_valid_idx - 1);
52 pfdset->num = last_valid_idx + 1;
56 * Find deleted fd entries and remove them
59 fdset_shrink(struct fdset *pfdset)
61 pthread_mutex_lock(&pfdset->fd_mutex);
62 fdset_shrink_nolock(pfdset);
63 pthread_mutex_unlock(&pfdset->fd_mutex);
67 * Returns the index in the fdset for a given fd.
69 * index for the fd, or -1 if fd isn't in the fdset.
72 fdset_find_fd(struct fdset *pfdset, int fd)
76 for (i = 0; i < pfdset->num && pfdset->fd[i].fd != fd; i++)
79 return i == pfdset->num ? -1 : i;
83 fdset_add_fd(struct fdset *pfdset, int idx, int fd,
84 fd_cb rcb, fd_cb wcb, void *dat)
86 struct fdentry *pfdentry = &pfdset->fd[idx];
87 struct pollfd *pfd = &pfdset->rwfds[idx];
95 pfd->events = rcb ? POLLIN : 0;
96 pfd->events |= wcb ? POLLOUT : 0;
101 fdset_init(struct fdset *pfdset)
108 for (i = 0; i < MAX_FDS; i++) {
109 pfdset->fd[i].fd = -1;
110 pfdset->fd[i].dat = NULL;
116 * Register the fd in the fdset with read/write handler and context.
119 fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
123 if (pfdset == NULL || fd == -1)
126 pthread_mutex_lock(&pfdset->fd_mutex);
127 i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
129 fdset_shrink_nolock(pfdset);
130 i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
132 pthread_mutex_unlock(&pfdset->fd_mutex);
137 fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
138 pthread_mutex_unlock(&pfdset->fd_mutex);
144 * Unregister the fd from the fdset.
145 * Returns context of a given fd or NULL.
148 fdset_del(struct fdset *pfdset, int fd)
153 if (pfdset == NULL || fd == -1)
157 pthread_mutex_lock(&pfdset->fd_mutex);
159 i = fdset_find_fd(pfdset, fd);
160 if (i != -1 && pfdset->fd[i].busy == 0) {
161 /* busy indicates r/wcb is executing! */
162 dat = pfdset->fd[i].dat;
163 pfdset->fd[i].fd = -1;
164 pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
165 pfdset->fd[i].dat = NULL;
168 pthread_mutex_unlock(&pfdset->fd_mutex);
176 * This functions runs in infinite blocking loop until there is no fd in
177 * pfdset. It calls corresponding r/w handler if there is event on the fd.
179 * Before the callback is called, we set the flag to busy status; If other
180 * thread(now rte_vhost_driver_unregister) calls fdset_del concurrently, it
181 * will wait until the flag is reset to zero(which indicates the callback is
182 * finished), then it could free the context after fdset_del.
185 fdset_event_dispatch(void *arg)
189 struct fdentry *pfdentry;
193 int remove1, remove2;
195 struct fdset *pfdset = arg;
204 * When poll is blocked, other threads might unregister
205 * listenfds from and register new listenfds into fdset.
206 * When poll returns, the entries for listenfds in the fdset
207 * might have been updated. It is ok if there is unwanted call
210 pthread_mutex_lock(&pfdset->fd_mutex);
211 numfds = pfdset->num;
212 pthread_mutex_unlock(&pfdset->fd_mutex);
214 val = poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
219 for (i = 0; i < numfds; i++) {
220 pthread_mutex_lock(&pfdset->fd_mutex);
222 pfdentry = &pfdset->fd[i];
224 pfd = &pfdset->rwfds[i];
228 pthread_mutex_unlock(&pfdset->fd_mutex);
233 pthread_mutex_unlock(&pfdset->fd_mutex);
237 remove1 = remove2 = 0;
244 pthread_mutex_unlock(&pfdset->fd_mutex);
246 if (rcb && pfd->revents & (POLLIN | FDPOLLERR))
247 rcb(fd, dat, &remove1);
248 if (wcb && pfd->revents & (POLLOUT | FDPOLLERR))
249 wcb(fd, dat, &remove2);
252 * fdset_del needs to check busy flag.
253 * We don't allow fdset_del to be called in callback
257 * When we are to clean up the fd from fdset,
258 * because the fd is closed in the cb,
259 * the old fd val could be reused by when creates new
260 * listen fd in another thread, we couldn't call
263 if (remove1 || remove2) {
270 fdset_shrink(pfdset);