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