1 |
adx |
30 |
/* |
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 |
knight |
31 |
* $Id$ |
24 |
adx |
30 |
*/ |
25 |
|
|
|
26 |
|
|
#include "stdinc.h" |
27 |
stu |
908 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_KQUEUE |
28 |
adx |
30 |
#include <sys/event.h> |
29 |
|
|
#include "fdlist.h" |
30 |
|
|
#include "ircd.h" |
31 |
|
|
#include "memory.h" |
32 |
|
|
#include "s_bsd.h" |
33 |
michael |
1309 |
#include "log.h" |
34 |
adx |
30 |
|
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 |
|
|
void init_netio(void); |
54 |
|
|
|
55 |
|
|
/* |
56 |
|
|
* init_netio |
57 |
|
|
* |
58 |
|
|
* This is a needed exported function which will be called to initialise |
59 |
|
|
* the network loop code. |
60 |
|
|
*/ |
61 |
|
|
void |
62 |
|
|
init_netio(void) |
63 |
|
|
{ |
64 |
|
|
int fd; |
65 |
|
|
|
66 |
|
|
if ((fd = kqueue()) < 0) |
67 |
|
|
{ |
68 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "init_netio: Couldn't open kqueue fd!"); |
69 |
adx |
30 |
exit(115); /* Whee! */ |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
fd_open(&kqfd, fd, 0, "kqueue() file descriptor"); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
/* |
76 |
|
|
* Write a single update to the kqueue list. |
77 |
|
|
*/ |
78 |
|
|
static void |
79 |
|
|
kq_update_events(int fd, int filter, int what) |
80 |
|
|
{ |
81 |
|
|
static struct timespec zero_timespec = {0, 0}; |
82 |
|
|
struct kevent *kep = kq_fdlist + kqoff; |
83 |
|
|
|
84 |
|
|
EV_SET(kep, (uintptr_t) fd, (short) filter, what, 0, 0, NULL); |
85 |
|
|
|
86 |
adx |
842 |
if (++kqoff == KE_LENGTH) |
87 |
adx |
30 |
{ |
88 |
michael |
2588 |
int i; |
89 |
|
|
|
90 |
|
|
for (i = 0; i < kqoff; ++i) |
91 |
|
|
kevent(kqfd.fd, kq_fdlist[i], 1, NULL, 0, &zero_timespec); |
92 |
adx |
30 |
kqoff = 0; |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
/* |
97 |
|
|
* comm_setselect |
98 |
|
|
* |
99 |
|
|
* This is a needed exported function which will be called to register |
100 |
|
|
* and deregister interest in a pending IO state for a given FD. |
101 |
|
|
*/ |
102 |
|
|
void |
103 |
|
|
comm_setselect(fde_t *F, unsigned int type, PF *handler, |
104 |
|
|
void *client_data, time_t timeout) |
105 |
|
|
{ |
106 |
|
|
int new_events, diff; |
107 |
|
|
|
108 |
|
|
if ((type & COMM_SELECT_READ)) |
109 |
|
|
{ |
110 |
|
|
F->read_handler = handler; |
111 |
|
|
F->read_data = client_data; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
if ((type & COMM_SELECT_WRITE)) |
115 |
|
|
{ |
116 |
|
|
F->write_handler = handler; |
117 |
|
|
F->write_data = client_data; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
new_events = (F->read_handler ? COMM_SELECT_READ : 0) | |
121 |
|
|
(F->write_handler ? COMM_SELECT_WRITE : 0); |
122 |
|
|
|
123 |
|
|
if (timeout != 0) |
124 |
|
|
F->timeout = CurrentTime + (timeout / 1000); |
125 |
|
|
|
126 |
|
|
diff = new_events ^ F->evcache; |
127 |
|
|
|
128 |
|
|
if ((diff & COMM_SELECT_READ)) |
129 |
|
|
kq_update_events(F->fd, EVFILT_READ, |
130 |
|
|
(new_events & COMM_SELECT_READ) ? EV_ADD : EV_DELETE); |
131 |
|
|
if ((diff & COMM_SELECT_WRITE)) |
132 |
|
|
kq_update_events(F->fd, EVFILT_WRITE, |
133 |
|
|
(new_events & COMM_SELECT_WRITE) ? EV_ADD : EV_DELETE); |
134 |
|
|
|
135 |
|
|
F->evcache = new_events; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/* |
139 |
|
|
* comm_select |
140 |
|
|
* |
141 |
|
|
* Called to do the new-style IO, courtesy of squid (like most of this |
142 |
|
|
* new IO code). This routine handles the stuff we've hidden in |
143 |
|
|
* comm_setselect and fd_table[] and calls callbacks for IO ready |
144 |
|
|
* events. |
145 |
|
|
*/ |
146 |
|
|
void |
147 |
|
|
comm_select(void) |
148 |
|
|
{ |
149 |
|
|
int num, i; |
150 |
|
|
static struct kevent ke[KE_LENGTH]; |
151 |
|
|
struct timespec poll_time; |
152 |
|
|
PF *hdl; |
153 |
|
|
fde_t *F; |
154 |
|
|
|
155 |
|
|
/* |
156 |
|
|
* remember we are doing NANOseconds here, not micro/milli. God knows |
157 |
|
|
* why jlemon used a timespec, but hey, he wrote the interface, not I |
158 |
|
|
* -- Adrian |
159 |
|
|
*/ |
160 |
|
|
poll_time.tv_sec = 0; |
161 |
|
|
poll_time.tv_nsec = SELECT_DELAY * 1000000; |
162 |
|
|
num = kevent(kqfd.fd, kq_fdlist, kqoff, ke, KE_LENGTH, &poll_time); |
163 |
|
|
kqoff = 0; |
164 |
|
|
|
165 |
|
|
set_time(); |
166 |
|
|
|
167 |
|
|
if (num < 0) |
168 |
|
|
{ |
169 |
|
|
#ifdef HAVE_USLEEP |
170 |
|
|
usleep(50000); /* avoid 99% CPU in comm_select */ |
171 |
|
|
#endif |
172 |
|
|
return; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
for (i = 0; i < num; i++) |
176 |
|
|
{ |
177 |
|
|
F = lookup_fd(ke[i].ident); |
178 |
|
|
if (F == NULL || !F->flags.open || (ke[i].flags & EV_ERROR)) |
179 |
|
|
continue; |
180 |
|
|
|
181 |
|
|
if (ke[i].filter == EVFILT_READ) |
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 |
|
|
if (ke[i].filter == EVFILT_WRITE) |
191 |
|
|
if ((hdl = F->write_handler) != NULL) |
192 |
|
|
{ |
193 |
|
|
F->write_handler = NULL; |
194 |
|
|
hdl(F, F->write_data); |
195 |
|
|
if (!F->flags.open) |
196 |
|
|
continue; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
comm_setselect(F, 0, NULL, NULL, 0); |
200 |
|
|
} |
201 |
|
|
} |
202 |
stu |
908 |
#endif |