8a075da224a25278fee4c26c5e7fe062362f048e
[deb_dpdk.git] / lib / librte_vhost / fd_man.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/socket.h>
38 #include <sys/time.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41 #include <string.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45
46 #include "fd_man.h"
47
48 #define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
49
50 static int
51 get_last_valid_idx(struct fdset *pfdset, int last_valid_idx)
52 {
53         int i;
54
55         for (i = last_valid_idx; i >= 0 && pfdset->fd[i].fd == -1; i--)
56                 ;
57
58         return i;
59 }
60
61 static void
62 fdset_move(struct fdset *pfdset, int dst, int src)
63 {
64         pfdset->fd[dst]    = pfdset->fd[src];
65         pfdset->rwfds[dst] = pfdset->rwfds[src];
66 }
67
68 /*
69  * Find deleted fd entries and remove them
70  */
71 static void
72 fdset_shrink(struct fdset *pfdset)
73 {
74         int i;
75         int last_valid_idx = get_last_valid_idx(pfdset, pfdset->num - 1);
76
77         pthread_mutex_lock(&pfdset->fd_mutex);
78
79         for (i = 0; i < last_valid_idx; i++) {
80                 if (pfdset->fd[i].fd != -1)
81                         continue;
82
83                 fdset_move(pfdset, i, last_valid_idx);
84                 last_valid_idx = get_last_valid_idx(pfdset, last_valid_idx - 1);
85         }
86         pfdset->num = last_valid_idx + 1;
87
88         pthread_mutex_unlock(&pfdset->fd_mutex);
89 }
90
91 /**
92  * Returns the index in the fdset for a given fd.
93  * @return
94  *   index for the fd, or -1 if fd isn't in the fdset.
95  */
96 static int
97 fdset_find_fd(struct fdset *pfdset, int fd)
98 {
99         int i;
100
101         for (i = 0; i < pfdset->num && pfdset->fd[i].fd != fd; i++)
102                 ;
103
104         return i == pfdset->num ? -1 : i;
105 }
106
107 static void
108 fdset_add_fd(struct fdset *pfdset, int idx, int fd,
109         fd_cb rcb, fd_cb wcb, void *dat)
110 {
111         struct fdentry *pfdentry = &pfdset->fd[idx];
112         struct pollfd *pfd = &pfdset->rwfds[idx];
113
114         pfdentry->fd  = fd;
115         pfdentry->rcb = rcb;
116         pfdentry->wcb = wcb;
117         pfdentry->dat = dat;
118
119         pfd->fd = fd;
120         pfd->events  = rcb ? POLLIN : 0;
121         pfd->events |= wcb ? POLLOUT : 0;
122         pfd->revents = 0;
123 }
124
125 void
126 fdset_init(struct fdset *pfdset)
127 {
128         int i;
129
130         if (pfdset == NULL)
131                 return;
132
133         for (i = 0; i < MAX_FDS; i++) {
134                 pfdset->fd[i].fd = -1;
135                 pfdset->fd[i].dat = NULL;
136         }
137         pfdset->num = 0;
138 }
139
140 /**
141  * Register the fd in the fdset with read/write handler and context.
142  */
143 int
144 fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
145 {
146         int i;
147
148         if (pfdset == NULL || fd == -1)
149                 return -1;
150
151         pthread_mutex_lock(&pfdset->fd_mutex);
152         i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
153         if (i == -1) {
154                 pthread_mutex_unlock(&pfdset->fd_mutex);
155                 return -2;
156         }
157
158         fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
159         pthread_mutex_unlock(&pfdset->fd_mutex);
160
161         return 0;
162 }
163
164 /**
165  *  Unregister the fd from the fdset.
166  *  Returns context of a given fd or NULL.
167  */
168 void *
169 fdset_del(struct fdset *pfdset, int fd)
170 {
171         int i;
172         void *dat = NULL;
173
174         if (pfdset == NULL || fd == -1)
175                 return NULL;
176
177         do {
178                 pthread_mutex_lock(&pfdset->fd_mutex);
179
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;
187                         i = -1;
188                 }
189                 pthread_mutex_unlock(&pfdset->fd_mutex);
190         } while (i != -1);
191
192         return dat;
193 }
194
195
196 /**
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.
199  *
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.
204  */
205 void
206 fdset_event_dispatch(struct fdset *pfdset)
207 {
208         int i;
209         struct pollfd *pfd;
210         struct fdentry *pfdentry;
211         fd_cb rcb, wcb;
212         void *dat;
213         int fd, numfds;
214         int remove1, remove2;
215         int need_shrink;
216
217         if (pfdset == NULL)
218                 return;
219
220         while (1) {
221
222                 /*
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
227                  * for new listenfds.
228                  */
229                 pthread_mutex_lock(&pfdset->fd_mutex);
230                 numfds = pfdset->num;
231                 pthread_mutex_unlock(&pfdset->fd_mutex);
232
233                 poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
234
235                 need_shrink = 0;
236                 for (i = 0; i < numfds; i++) {
237                         pthread_mutex_lock(&pfdset->fd_mutex);
238
239                         pfdentry = &pfdset->fd[i];
240                         fd = pfdentry->fd;
241                         pfd = &pfdset->rwfds[i];
242
243                         if (fd < 0) {
244                                 need_shrink = 1;
245                                 pthread_mutex_unlock(&pfdset->fd_mutex);
246                                 continue;
247                         }
248
249                         if (!pfd->revents) {
250                                 pthread_mutex_unlock(&pfdset->fd_mutex);
251                                 continue;
252                         }
253
254                         remove1 = remove2 = 0;
255
256                         rcb = pfdentry->rcb;
257                         wcb = pfdentry->wcb;
258                         dat = pfdentry->dat;
259                         pfdentry->busy = 1;
260
261                         pthread_mutex_unlock(&pfdset->fd_mutex);
262
263                         if (rcb && pfd->revents & (POLLIN | FDPOLLERR))
264                                 rcb(fd, dat, &remove1);
265                         if (wcb && pfd->revents & (POLLOUT | FDPOLLERR))
266                                 wcb(fd, dat, &remove2);
267                         pfdentry->busy = 0;
268                         /*
269                          * fdset_del needs to check busy flag.
270                          * We don't allow fdset_del to be called in callback
271                          * directly.
272                          */
273                         /*
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
278                          * fd_set_del.
279                          */
280                         if (remove1 || remove2) {
281                                 pfdentry->fd = -1;
282                                 need_shrink = 1;
283                         }
284                 }
285
286                 if (need_shrink)
287                         fdset_shrink(pfdset);
288         }
289 }