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>
38 #include <sys/select.h>
40 #include <sys/types.h>
43 #include <rte_common.h>
49 * Returns the index in the fdset for a given fd.
50 * If fd is -1, it means to search for a free entry.
52 * index for the fd, or -1 if fd isn't in the fdset.
55 fdset_find_fd(struct fdset *pfdset, int fd)
62 for (i = 0; i < MAX_FDS && pfdset->fd[i].fd != fd; i++)
65 return i == MAX_FDS ? -1 : i;
69 fdset_find_free_slot(struct fdset *pfdset)
71 return fdset_find_fd(pfdset, -1);
75 fdset_add_fd(struct fdset *pfdset, int idx, int fd,
76 fd_cb rcb, fd_cb wcb, void *dat)
78 struct fdentry *pfdentry;
80 if (pfdset == NULL || idx >= MAX_FDS || fd >= FD_SETSIZE)
83 pfdentry = &pfdset->fd[idx];
93 * Fill the read/write fd_set with the fds in the fdset.
95 * the maximum fds filled in the read/write fd_set.
98 fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
100 struct fdentry *pfdentry;
107 for (i = 0; i < num; i++) {
108 pfdentry = &pfdset->fd[i];
109 if (pfdentry->fd != -1) {
111 if (pfdentry->rcb && rfset) {
112 FD_SET(pfdentry->fd, rfset);
115 if (pfdentry->wcb && wfset) {
116 FD_SET(pfdentry->fd, wfset);
120 maxfds = pfdentry->fd < maxfds ?
121 maxfds : pfdentry->fd;
128 fdset_init(struct fdset *pfdset)
135 for (i = 0; i < MAX_FDS; i++)
136 pfdset->fd[i].fd = -1;
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);
153 /* Find a free slot in the list. */
154 i = fdset_find_free_slot(pfdset);
155 if (i == -1 || fdset_add_fd(pfdset, i, fd, rcb, wcb, dat) < 0) {
156 pthread_mutex_unlock(&pfdset->fd_mutex);
162 pthread_mutex_unlock(&pfdset->fd_mutex);
168 * Unregister the fd from the fdset.
171 fdset_del(struct fdset *pfdset, int fd)
175 if (pfdset == NULL || fd == -1)
179 pthread_mutex_lock(&pfdset->fd_mutex);
181 i = fdset_find_fd(pfdset, fd);
182 if (i != -1 && pfdset->fd[i].busy == 0) {
183 /* busy indicates r/wcb is executing! */
184 pfdset->fd[i].fd = -1;
185 pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
189 pthread_mutex_unlock(&pfdset->fd_mutex);
194 * Unregister the fd at the specified slot from the fdset.
197 fdset_del_slot(struct fdset *pfdset, int index)
199 if (pfdset == NULL || index < 0 || index >= MAX_FDS)
202 pthread_mutex_lock(&pfdset->fd_mutex);
204 pfdset->fd[index].fd = -1;
205 pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL;
208 pthread_mutex_unlock(&pfdset->fd_mutex);
212 * This functions runs in infinite blocking loop until there is no fd in
213 * pfdset. It calls corresponding r/w handler if there is event on the fd.
215 * Before the callback is called, we set the flag to busy status; If other
216 * thread(now rte_vhost_driver_unregister) calls fdset_del concurrently, it
217 * will wait until the flag is reset to zero(which indicates the callback is
218 * finished), then it could free the context after fdset_del.
221 fdset_event_dispatch(struct fdset *pfdset)
225 struct fdentry *pfdentry;
230 int remove1, remove2;
242 pthread_mutex_lock(&pfdset->fd_mutex);
244 maxfds = fdset_fill(&rfds, &wfds, pfdset);
246 pthread_mutex_unlock(&pfdset->fd_mutex);
249 * When select is blocked, other threads might unregister
250 * listenfds from and register new listenfds into fdset.
251 * When select returns, the entries for listenfds in the fdset
252 * might have been updated. It is ok if there is unwanted call
255 ret = select(maxfds + 1, &rfds, &wfds, NULL, &tv);
259 for (i = 0; i < num; i++) {
260 remove1 = remove2 = 0;
261 pthread_mutex_lock(&pfdset->fd_mutex);
262 pfdentry = &pfdset->fd[i];
268 pthread_mutex_unlock(&pfdset->fd_mutex);
269 if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb)
270 rcb(fd, dat, &remove1);
271 if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb)
272 wcb(fd, dat, &remove2);
275 * fdset_del needs to check busy flag.
276 * We don't allow fdset_del to be called in callback
280 * When we are to clean up the fd from fdset,
281 * because the fd is closed in the cb,
282 * the old fd val could be reused by when creates new
283 * listen fd in another thread, we couldn't call
286 if (remove1 || remove2)
287 fdset_del_slot(pfdset, i);