]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/mevent.c
IFC @ r225592
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / mevent.c
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * Micro event library for FreeBSD, designed for a single i/o thread 
31  * using kqueue, and having events be persistent by default.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <assert.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include <sys/types.h>
45 #include <sys/event.h>
46 #include <sys/time.h>
47
48 #include <pthread.h>
49
50 #include "mevent.h"
51
52 #define MEVENT_MAX      64
53
54 #define MEV_ENABLE      1
55 #define MEV_DISABLE     2
56 #define MEV_DEL_PENDING 3
57
58 static pthread_t mevent_tid;
59 static int mevent_pipefd[2];
60 static pthread_mutex_t mevent_lmutex = PTHREAD_MUTEX_INITIALIZER;
61
62 struct mevent { 
63         void    (*me_func)(int, enum ev_type, void *);
64         int     me_fd;
65         enum ev_type me_type;
66         void    *me_param;
67         int     me_cq;
68         int     me_state;
69         int     me_closefd;
70         LIST_ENTRY(mevent) me_list;                        
71 };
72
73 static LIST_HEAD(listhead, mevent) global_head, change_head;
74
75 static void
76 mevent_qlock(void)
77 {
78         pthread_mutex_lock(&mevent_lmutex);
79 }
80
81 static void
82 mevent_qunlock(void)
83 {
84         pthread_mutex_unlock(&mevent_lmutex);
85 }
86
87 static void
88 mevent_pipe_read(int fd, enum ev_type type, void *param)
89 {
90         char buf[MEVENT_MAX];
91         int status;
92
93         /*
94          * Drain the pipe read side. The fd is non-blocking so this is
95          * safe to do.
96          */
97         do {
98                 status = read(fd, buf, sizeof(buf));
99         } while (status == MEVENT_MAX);
100 }
101
102 static void
103 mevent_notify(void)
104 {
105         char c;
106         
107         /*
108          * If calling from outside the i/o thread, write a byte on the
109          * pipe to force the i/o thread to exit the blocking kevent call.
110          */
111         if (mevent_pipefd[1] != 0 && pthread_self() != mevent_tid) {
112                 write(mevent_pipefd[1], &c, 1);
113         }
114 }
115
116 static int
117 mevent_kq_filter(struct mevent *mevp)
118 {
119         int retval;
120
121         retval = 0;
122
123         if (mevp->me_type == EVF_READ)
124                 retval = EVFILT_READ;
125
126         if (mevp->me_type == EVF_WRITE)
127                 retval = EVFILT_WRITE;
128
129         return (retval);
130 }
131
132 static int
133 mevent_kq_flags(struct mevent *mevp)
134 {
135         int ret;
136
137         switch (mevp->me_state) {
138         case MEV_ENABLE:
139                 ret = EV_ADD;
140                 break;
141         case MEV_DISABLE:
142                 ret = EV_DISABLE;
143                 break;
144         case MEV_DEL_PENDING:
145                 ret = EV_DELETE;
146                 break;
147         }
148
149         return (ret);
150 }
151
152 static int
153 mevent_kq_fflags(struct mevent *mevp)
154 {
155         /* XXX nothing yet, perhaps EV_EOF for reads ? */
156         return (0);
157 }
158
159 static int
160 mevent_build(int mfd, struct kevent *kev)
161 {
162         struct mevent *mevp, *tmpp;
163         int i;
164
165         i = 0;
166
167         mevent_qlock();
168
169         LIST_FOREACH_SAFE(mevp, &change_head, me_list, tmpp) {
170                 if (mevp->me_closefd) {
171                         /*
172                          * A close of the file descriptor will remove the
173                          * event
174                          */
175                         close(mevp->me_fd);
176                 } else {
177                         kev[i].ident = mevp->me_fd;
178                         kev[i].filter = mevent_kq_filter(mevp);
179                         kev[i].flags = mevent_kq_flags(mevp);
180                         kev[i].fflags = mevent_kq_fflags(mevp);
181                         kev[i].data = 0;
182                         kev[i].udata = mevp;
183                         i++;
184                 }
185
186                 mevp->me_cq = 0;
187                 LIST_REMOVE(mevp, me_list);
188
189                 if (mevp->me_state == MEV_DEL_PENDING) {
190                         free(mevp);
191                 } else {
192                         LIST_INSERT_HEAD(&global_head, mevp, me_list);
193                 }
194
195                 assert(i < MEVENT_MAX);
196         }
197
198         mevent_qunlock();
199
200         return (i);
201 }
202
203 static void
204 mevent_handle(struct kevent *kev, int numev)
205 {
206         struct mevent *mevp;
207         int i;
208
209         for (i = 0; i < numev; i++) {
210                 mevp = kev[i].udata;
211
212                 /* XXX check for EV_ERROR ? */
213
214                 (*mevp->me_func)(mevp->me_fd, mevp->me_type, mevp->me_param);
215         }
216 }
217
218 struct mevent *
219 mevent_add(int fd, enum ev_type type,
220            void (*func)(int, enum ev_type, void *), void *param)
221 {
222         struct mevent *lp, *mevp;
223
224         if (fd < 0 || func == NULL) {
225                 return (NULL);
226         }
227
228         mevp = NULL;
229
230         mevent_qlock();
231
232         /*
233          * Verify that the fd/type tuple is not present in any list
234          */
235         LIST_FOREACH(lp, &global_head, me_list) {
236                 if (lp->me_fd == fd && lp->me_type == type) {
237                         goto exit;
238                 }
239         }
240
241         LIST_FOREACH(lp, &change_head, me_list) {
242                 if (lp->me_fd == fd && lp->me_type == type) {
243                         goto exit;
244                 }
245         }
246
247         /*
248          * Allocate an entry, populate it, and add it to the change list.
249          */
250         mevp = malloc(sizeof(struct mevent));
251         if (mevp == NULL) {
252                 goto exit;
253         }
254
255         memset(mevp, 0, sizeof(struct mevent));
256         mevp->me_fd = fd;
257         mevp->me_type = type;
258         mevp->me_func = func;
259         mevp->me_param = param;
260
261         LIST_INSERT_HEAD(&change_head, mevp, me_list);
262         mevp->me_cq = 1;
263         mevp->me_state = MEV_ENABLE;
264         mevent_notify();
265
266 exit:
267         mevent_qunlock();
268
269         return (mevp);
270 }
271
272 static int
273 mevent_update(struct mevent *evp, int newstate)
274 {
275         /*
276          * It's not possible to enable/disable a deleted event
277          */
278         if (evp->me_state == MEV_DEL_PENDING)
279                 return (EINVAL);
280
281         /*
282          * No update needed if state isn't changing
283          */
284         if (evp->me_state == newstate)
285                 return (0);
286         
287         mevent_qlock();
288
289         evp->me_state = newstate;
290
291         /*
292          * Place the entry onto the changed list if not already there.
293          */
294         if (evp->me_cq == 0) {
295                 evp->me_cq = 1;
296                 LIST_REMOVE(evp, me_list);
297                 LIST_INSERT_HEAD(&change_head, evp, me_list);
298                 mevent_notify();
299         }
300
301         mevent_qunlock();
302
303         return (0);
304 }
305
306 int
307 mevent_enable(struct mevent *evp)
308 {
309
310         return (mevent_update(evp, MEV_ENABLE));
311 }
312
313 int
314 mevent_disable(struct mevent *evp)
315 {
316
317         return (mevent_update(evp, MEV_DISABLE));
318 }
319
320 static int
321 mevent_delete_event(struct mevent *evp, int closefd)
322 {
323         mevent_qlock();
324
325         /*
326          * Place the entry onto the changed list if not already there, and
327          * mark as to be deleted.
328          */
329         if (evp->me_cq == 0) {
330                 evp->me_cq = 1;
331                 LIST_REMOVE(evp, me_list);
332                 LIST_INSERT_HEAD(&change_head, evp, me_list);
333                 mevent_notify();
334         }
335         evp->me_state = MEV_DEL_PENDING;
336
337         if (closefd)
338                 evp->me_closefd = 1;
339
340         mevent_qunlock();
341
342         return (0);
343 }
344
345 int
346 mevent_delete(struct mevent *evp)
347 {
348
349         return (mevent_delete_event(evp, 0));
350 }
351
352 int
353 mevent_delete_close(struct mevent *evp)
354 {
355
356         return (mevent_delete_event(evp, 1));
357 }
358
359 void
360 mevent_dispatch(void)
361 {
362         struct kevent changelist[MEVENT_MAX];
363         struct kevent eventlist[MEVENT_MAX];
364         struct mevent *pipev;
365         int mfd;
366         int numev;
367         int ret;
368
369         mevent_tid = pthread_self();
370
371         mfd = kqueue();
372         assert(mfd > 0);
373
374         /*
375          * Open the pipe that will be used for other threads to force
376          * the blocking kqueue call to exit by writing to it. Set the
377          * descriptor to non-blocking.
378          */
379         ret = pipe(mevent_pipefd);
380         if (ret < 0) {
381                 perror("pipe");
382                 exit(0);
383         }
384
385         /*
386          * Add internal event handler for the pipe write fd
387          */
388         pipev = mevent_add(mevent_pipefd[0], EVF_READ, mevent_pipe_read, NULL);
389         assert(pipev != NULL);
390
391         for (;;) {
392                 /*
393                  * Build changelist if required.
394                  * XXX the changelist can be put into the blocking call
395                  * to eliminate the extra syscall. Currently better for
396                  * debug.
397                  */
398                 numev = mevent_build(mfd, changelist);
399                 if (numev) {
400                         ret = kevent(mfd, changelist, numev, NULL, 0, NULL);
401                         if (ret == -1) {
402                                 perror("Error return from kevent change");
403                         }
404                 }
405
406                 /*
407                  * Block awaiting events
408                  */
409                 ret = kevent(mfd, NULL, 0, eventlist, MEVENT_MAX, NULL);
410                 if (ret == -1) {
411                         perror("Error return from kevent monitor");
412                 }
413                 
414                 /*
415                  * Handle reported events
416                  */
417                 mevent_handle(eventlist, ret);
418         }                       
419 }