4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/socket.h>
39 #include <sys/types.h>
43 #include <rte_common.h>
48 #define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
51 get_last_valid_idx(struct fdset *pfdset, int last_valid_idx)
55 for (i = last_valid_idx; i >= 0 && pfdset->fd[i].fd == -1; i--)
62 fdset_move(struct fdset *pfdset, int dst, int src)
64 pfdset->fd[dst] = pfdset->fd[src];
65 pfdset->rwfds[dst] = pfdset->rwfds[src];
69 * Find deleted fd entries and remove them
72 fdset_shrink(struct fdset *pfdset)
75 int last_valid_idx = get_last_valid_idx(pfdset, pfdset->num - 1);
77 pthread_mutex_lock(&pfdset->fd_mutex);
79 for (i = 0; i < last_valid_idx; i++) {
80 if (pfdset->fd[i].fd != -1)
83 fdset_move(pfdset, i, last_valid_idx);
84 last_valid_idx = get_last_valid_idx(pfdset, last_valid_idx - 1);
86 pfdset->num = last_valid_idx + 1;
88 pthread_mutex_unlock(&pfdset->fd_mutex);
92 * Returns the index in the fdset for a given fd.
94 * index for the fd, or -1 if fd isn't in the fdset.
97 fdset_find_fd(struct fdset *pfdset, int fd)
101 for (i = 0; i < pfdset->num && pfdset->fd[i].fd != fd; i++)
104 return i == pfdset->num ? -1 : i;
108 fdset_add_fd(struct fdset *pfdset, int idx, int fd,
109 fd_cb rcb, fd_cb wcb, void *dat)
111 struct fdentry *pfdentry = &pfdset->fd[idx];
112 struct pollfd *pfd = &pfdset->rwfds[idx];
120 pfd->events = rcb ? POLLIN : 0;
121 pfd->events |= wcb ? POLLOUT : 0;
126 fdset_init(struct fdset *pfdset)
133 for (i = 0; i < MAX_FDS; i++) {
134 pfdset->fd[i].fd = -1;
135 pfdset->fd[i].dat = NULL;
141 * Register the fd in the fdset with read/write handler and context.
144 fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
148 if (pfdset == NULL || fd == -1)
151 pthread_mutex_lock(&pfdset->fd_mutex);
152 i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
154 pthread_mutex_unlock(&pfdset->fd_mutex);
158 fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
159 pthread_mutex_unlock(&pfdset->fd_mutex);
165 * Unregister the fd from the fdset.
166 * Returns context of a given fd or NULL.
169 fdset_del(struct fdset *pfdset, int fd)
174 if (pfdset == NULL || fd == -1)
178 pthread_mutex_lock(&pfdset->fd_mutex);
180 i = fdset_find_fd(pfdset, fd);
181 if (i != -1 && pfdset->fd[i].busy == 0) {
182 /* busy indicates r/wcb is executing! */
183 dat = pfdset->fd[i].dat;
184 pfdset->fd[i].fd = -1;
185 pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
186 pfdset->fd[i].dat = NULL;
189 pthread_mutex_unlock(&pfdset->fd_mutex);
197 * This functions runs in infinite blocking loop until there is no fd in
198 * pfdset. It calls corresponding r/w handler if there is event on the fd.
200 * Before the callback is called, we set the flag to busy status; If other
201 * thread(now rte_vhost_driver_unregister) calls fdset_del concurrently, it
202 * will wait until the flag is reset to zero(which indicates the callback is
203 * finished), then it could free the context after fdset_del.
206 fdset_event_dispatch(struct fdset *pfdset)
210 struct fdentry *pfdentry;
214 int remove1, remove2;
223 * When poll is blocked, other threads might unregister
224 * listenfds from and register new listenfds into fdset.
225 * When poll returns, the entries for listenfds in the fdset
226 * might have been updated. It is ok if there is unwanted call
229 pthread_mutex_lock(&pfdset->fd_mutex);
230 numfds = pfdset->num;
231 pthread_mutex_unlock(&pfdset->fd_mutex);
233 poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
236 for (i = 0; i < numfds; i++) {
237 pthread_mutex_lock(&pfdset->fd_mutex);
239 pfdentry = &pfdset->fd[i];
241 pfd = &pfdset->rwfds[i];
245 pthread_mutex_unlock(&pfdset->fd_mutex);
250 pthread_mutex_unlock(&pfdset->fd_mutex);
254 remove1 = remove2 = 0;
261 pthread_mutex_unlock(&pfdset->fd_mutex);
263 if (rcb && pfd->revents & (POLLIN | FDPOLLERR))
264 rcb(fd, dat, &remove1);
265 if (wcb && pfd->revents & (POLLOUT | FDPOLLERR))
266 wcb(fd, dat, &remove2);
269 * fdset_del needs to check busy flag.
270 * We don't allow fdset_del to be called in callback
274 * When we are to clean up the fd from fdset,
275 * because the fd is closed in the cb,
276 * the old fd val could be reused by when creates new
277 * listen fd in another thread, we couldn't call
280 if (remove1 || remove2) {
287 fdset_shrink(pfdset);