1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* s_bsd_kqueue.c: FreeBSD kqueue compatible network routines. |
4 |
* |
5 |
* Originally by Adrian Chadd <adrian@creative.net.au> |
6 |
* Copyright (C) 2005 Hybrid Development Team |
7 |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
10 |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
21 |
* USA |
22 |
* |
23 |
* $Id$ |
24 |
*/ |
25 |
|
26 |
#include "stdinc.h" |
27 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_KQUEUE |
28 |
#include <sys/event.h> |
29 |
#include "fdlist.h" |
30 |
#include "ircd.h" |
31 |
#include "memory.h" |
32 |
#include "s_bsd.h" |
33 |
#include "log.h" |
34 |
|
35 |
#define KE_LENGTH 128 |
36 |
|
37 |
/* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */ |
38 |
|
39 |
#ifndef EV_SET |
40 |
#define EV_SET(kevp, a, b, c, d, e, f) do { \ |
41 |
(kevp)->ident = (a); \ |
42 |
(kevp)->filter = (b); \ |
43 |
(kevp)->flags = (c); \ |
44 |
(kevp)->fflags = (d); \ |
45 |
(kevp)->data = (e); \ |
46 |
(kevp)->udata = (f); \ |
47 |
} while(0) |
48 |
#endif |
49 |
|
50 |
static fde_t kqfd; |
51 |
static struct kevent kq_fdlist[KE_LENGTH]; /* kevent buffer */ |
52 |
static int kqoff; /* offset into the buffer */ |
53 |
|
54 |
/* |
55 |
* init_netio |
56 |
* |
57 |
* This is a needed exported function which will be called to initialise |
58 |
* the network loop code. |
59 |
*/ |
60 |
void |
61 |
init_netio(void) |
62 |
{ |
63 |
int fd; |
64 |
|
65 |
if ((fd = kqueue()) < 0) |
66 |
{ |
67 |
ilog(LOG_TYPE_IRCD, "init_netio: Couldn't open kqueue fd!"); |
68 |
exit(115); /* Whee! */ |
69 |
} |
70 |
|
71 |
fd_open(&kqfd, fd, 0, "kqueue() file descriptor"); |
72 |
} |
73 |
|
74 |
/* |
75 |
* Write a single update to the kqueue list. |
76 |
*/ |
77 |
static void |
78 |
kq_update_events(int fd, int filter, int what) |
79 |
{ |
80 |
static struct timespec zero_timespec = {0, 0}; |
81 |
struct kevent *kep = kq_fdlist + kqoff; |
82 |
|
83 |
EV_SET(kep, (uintptr_t) fd, (short) filter, what, 0, 0, NULL); |
84 |
|
85 |
if (++kqoff == KE_LENGTH) |
86 |
{ |
87 |
int i; |
88 |
|
89 |
for (i = 0; i < kqoff; ++i) |
90 |
kevent(kqfd.fd, &kq_fdlist[i], 1, NULL, 0, &zero_timespec); |
91 |
kqoff = 0; |
92 |
} |
93 |
} |
94 |
|
95 |
/* |
96 |
* comm_setselect |
97 |
* |
98 |
* This is a needed exported function which will be called to register |
99 |
* and deregister interest in a pending IO state for a given FD. |
100 |
*/ |
101 |
void |
102 |
comm_setselect(fde_t *F, unsigned int type, PF *handler, |
103 |
void *client_data, time_t timeout) |
104 |
{ |
105 |
int new_events, diff; |
106 |
|
107 |
if ((type & COMM_SELECT_READ)) |
108 |
{ |
109 |
F->read_handler = handler; |
110 |
F->read_data = client_data; |
111 |
} |
112 |
|
113 |
if ((type & COMM_SELECT_WRITE)) |
114 |
{ |
115 |
F->write_handler = handler; |
116 |
F->write_data = client_data; |
117 |
} |
118 |
|
119 |
new_events = (F->read_handler ? COMM_SELECT_READ : 0) | |
120 |
(F->write_handler ? COMM_SELECT_WRITE : 0); |
121 |
|
122 |
if (timeout != 0) |
123 |
F->timeout = CurrentTime + (timeout / 1000); |
124 |
|
125 |
diff = new_events ^ F->evcache; |
126 |
|
127 |
if ((diff & COMM_SELECT_READ)) |
128 |
kq_update_events(F->fd, EVFILT_READ, |
129 |
(new_events & COMM_SELECT_READ) ? EV_ADD : EV_DELETE); |
130 |
if ((diff & COMM_SELECT_WRITE)) |
131 |
kq_update_events(F->fd, EVFILT_WRITE, |
132 |
(new_events & COMM_SELECT_WRITE) ? EV_ADD : EV_DELETE); |
133 |
|
134 |
F->evcache = new_events; |
135 |
} |
136 |
|
137 |
/* |
138 |
* comm_select |
139 |
* |
140 |
* Called to do the new-style IO, courtesy of squid (like most of this |
141 |
* new IO code). This routine handles the stuff we've hidden in |
142 |
* comm_setselect and fd_table[] and calls callbacks for IO ready |
143 |
* events. |
144 |
*/ |
145 |
void |
146 |
comm_select(void) |
147 |
{ |
148 |
int num, i; |
149 |
static struct kevent ke[KE_LENGTH]; |
150 |
struct timespec poll_time; |
151 |
PF *hdl; |
152 |
fde_t *F; |
153 |
|
154 |
/* |
155 |
* remember we are doing NANOseconds here, not micro/milli. God knows |
156 |
* why jlemon used a timespec, but hey, he wrote the interface, not I |
157 |
* -- Adrian |
158 |
*/ |
159 |
poll_time.tv_sec = 0; |
160 |
poll_time.tv_nsec = SELECT_DELAY * 1000000; |
161 |
num = kevent(kqfd.fd, kq_fdlist, kqoff, ke, KE_LENGTH, &poll_time); |
162 |
kqoff = 0; |
163 |
|
164 |
set_time(); |
165 |
|
166 |
if (num < 0) |
167 |
{ |
168 |
#ifdef HAVE_USLEEP |
169 |
usleep(50000); /* avoid 99% CPU in comm_select */ |
170 |
#endif |
171 |
return; |
172 |
} |
173 |
|
174 |
for (i = 0; i < num; i++) |
175 |
{ |
176 |
F = lookup_fd(ke[i].ident); |
177 |
if (F == NULL || !F->flags.open || (ke[i].flags & EV_ERROR)) |
178 |
continue; |
179 |
|
180 |
if (ke[i].filter == EVFILT_READ) |
181 |
{ |
182 |
if ((hdl = F->read_handler) != NULL) |
183 |
{ |
184 |
F->read_handler = NULL; |
185 |
hdl(F, F->read_data); |
186 |
if (!F->flags.open) |
187 |
continue; |
188 |
} |
189 |
} |
190 |
|
191 |
if (ke[i].filter == EVFILT_WRITE) |
192 |
{ |
193 |
if ((hdl = F->write_handler) != NULL) |
194 |
{ |
195 |
F->write_handler = NULL; |
196 |
hdl(F, F->write_data); |
197 |
if (!F->flags.open) |
198 |
continue; |
199 |
} |
200 |
} |
201 |
|
202 |
comm_setselect(F, 0, NULL, NULL, 0); |
203 |
} |
204 |
} |
205 |
#endif |