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 fdset_shrink_nolock(struct fdset *pfdset)
72 int last_valid_idx = get_last_valid_idx(pfdset, pfdset->num - 1);
74 for (i = 0; i < last_valid_idx; i++) {
75 if (pfdset->fd[i].fd != -1)
78 fdset_move(pfdset, i, last_valid_idx);
79 last_valid_idx = get_last_valid_idx(pfdset, last_valid_idx - 1);
81 pfdset->num = last_valid_idx + 1;
85 * Find deleted fd entries and remove them
88 fdset_shrink(struct fdset *pfdset)
90 pthread_mutex_lock(&pfdset->fd_mutex);
91 fdset_shrink_nolock(pfdset);
92 pthread_mutex_unlock(&pfdset->fd_mutex);
96 * Returns the index in the fdset for a given fd.
98 * index for the fd, or -1 if fd isn't in the fdset.
101 fdset_find_fd(struct fdset *pfdset, int fd)
105 for (i = 0; i < pfdset->num && pfdset->fd[i].fd != fd; i++)
108 return i == pfdset->num ? -1 : i;
112 fdset_add_fd(struct fdset *pfdset, int idx, int fd,
113 fd_cb rcb, fd_cb wcb, void *dat)
115 struct fdentry *pfdentry = &pfdset->fd[idx];
116 struct pollfd *pfd = &pfdset->rwfds[idx];
124 pfd->events = rcb ? POLLIN : 0;
125 pfd->events |= wcb ? POLLOUT : 0;
130 fdset_init(struct fdset *pfdset)
137 for (i = 0; i < MAX_FDS; i++) {
138 pfdset->fd[i].fd = -1;
139 pfdset->fd[i].dat = NULL;
145 * Register the fd in the fdset with read/write handler and context.
148 fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
152 if (pfdset == NULL || fd == -1)
155 pthread_mutex_lock(&pfdset->fd_mutex);
156 i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
158 fdset_shrink_nolock(pfdset);
159 i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
161 pthread_mutex_unlock(&pfdset->fd_mutex);
166 fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
167 pthread_mutex_unlock(&pfdset->fd_mutex);
173 * Unregister the fd from the fdset.
174 * Returns context of a given fd or NULL.
177 fdset_del(struct fdset *pfdset, int fd)
182 if (pfdset == NULL || fd == -1)
186 pthread_mutex_lock(&pfdset->fd_mutex);
188 i = fdset_find_fd(pfdset, fd);
189 if (i != -1 && pfdset->fd[i].busy == 0) {
190 /* busy indicates r/wcb is executing! */
191 dat = pfdset->fd[i].dat;
192 pfdset->fd[i].fd = -1;
193 pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
194 pfdset->fd[i].dat = NULL;
197 pthread_mutex_unlock(&pfdset->fd_mutex);
205 * This functions runs in infinite blocking loop until there is no fd in
206 * pfdset. It calls corresponding r/w handler if there is event on the fd.
208 * Before the callback is called, we set the flag to busy status; If other
209 * thread(now rte_vhost_driver_unregister) calls fdset_del concurrently, it
210 * will wait until the flag is reset to zero(which indicates the callback is
211 * finished), then it could free the context after fdset_del.
214 fdset_event_dispatch(void *arg)
218 struct fdentry *pfdentry;
222 int remove1, remove2;
224 struct fdset *pfdset = arg;
232 * When poll is blocked, other threads might unregister
233 * listenfds from and register new listenfds into fdset.
234 * When poll returns, the entries for listenfds in the fdset
235 * might have been updated. It is ok if there is unwanted call
238 pthread_mutex_lock(&pfdset->fd_mutex);
239 numfds = pfdset->num;
240 pthread_mutex_unlock(&pfdset->fd_mutex);
242 poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
245 for (i = 0; i < numfds; i++) {
246 pthread_mutex_lock(&pfdset->fd_mutex);
248 pfdentry = &pfdset->fd[i];
250 pfd = &pfdset->rwfds[i];
254 pthread_mutex_unlock(&pfdset->fd_mutex);
259 pthread_mutex_unlock(&pfdset->fd_mutex);
263 remove1 = remove2 = 0;
270 pthread_mutex_unlock(&pfdset->fd_mutex);
272 if (rcb && pfd->revents & (POLLIN | FDPOLLERR))
273 rcb(fd, dat, &remove1);
274 if (wcb && pfd->revents & (POLLOUT | FDPOLLERR))
275 wcb(fd, dat, &remove2);
278 * fdset_del needs to check busy flag.
279 * We don't allow fdset_del to be called in callback
283 * When we are to clean up the fd from fdset,
284 * because the fd is closed in the cb,
285 * the old fd val could be reused by when creates new
286 * listen fd in another thread, we couldn't call
289 if (remove1 || remove2) {
296 fdset_shrink(pfdset);