1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2000-2014 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file s_bsd_kqueue.c |
23 |
* \brief kqueue() compatible network routines. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_KQUEUE |
29 |
#include <sys/event.h> |
30 |
#include "fdlist.h" |
31 |
#include "ircd.h" |
32 |
#include "memory.h" |
33 |
#include "s_bsd.h" |
34 |
#include "log.h" |
35 |
|
36 |
#define KE_LENGTH 128 |
37 |
|
38 |
/* jlemon goofed up and didn't add EV_SET until fbsd 4.3 */ |
39 |
|
40 |
#ifndef EV_SET |
41 |
#define EV_SET(kevp, a, b, c, d, e, f) do { \ |
42 |
(kevp)->ident = (a); \ |
43 |
(kevp)->filter = (b); \ |
44 |
(kevp)->flags = (c); \ |
45 |
(kevp)->fflags = (d); \ |
46 |
(kevp)->data = (e); \ |
47 |
(kevp)->udata = (f); \ |
48 |
} while(0) |
49 |
#endif |
50 |
|
51 |
static fde_t kqfd; |
52 |
static struct kevent kq_fdlist[KE_LENGTH]; /* kevent buffer */ |
53 |
static int kqoff; /* offset into the buffer */ |
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 |
ilog(LOG_TYPE_IRCD, "init_netio: Couldn't open kqueue fd!"); |
69 |
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 |
if (++kqoff == KE_LENGTH) |
87 |
{ |
88 |
int i; |
89 |
|
90 |
for (i = 0; i < kqoff; ++i) |
91 |
kevent(kqfd.fd, &kq_fdlist[i], 1, NULL, 0, &zero_timespec); |
92 |
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 |
{ |
125 |
F->timeout = CurrentTime + (timeout / 1000); |
126 |
F->timeout_handler = handler; |
127 |
F->timeout_data = client_data; |
128 |
} |
129 |
|
130 |
diff = new_events ^ F->evcache; |
131 |
|
132 |
if ((diff & COMM_SELECT_READ)) |
133 |
kq_update_events(F->fd, EVFILT_READ, |
134 |
(new_events & COMM_SELECT_READ) ? EV_ADD : EV_DELETE); |
135 |
if ((diff & COMM_SELECT_WRITE)) |
136 |
kq_update_events(F->fd, EVFILT_WRITE, |
137 |
(new_events & COMM_SELECT_WRITE) ? EV_ADD : EV_DELETE); |
138 |
|
139 |
F->evcache = new_events; |
140 |
} |
141 |
|
142 |
/* |
143 |
* comm_select |
144 |
* |
145 |
* Called to do the new-style IO, courtesy of squid (like most of this |
146 |
* new IO code). This routine handles the stuff we've hidden in |
147 |
* comm_setselect and fd_table[] and calls callbacks for IO ready |
148 |
* events. |
149 |
*/ |
150 |
void |
151 |
comm_select(void) |
152 |
{ |
153 |
int num, i; |
154 |
static struct kevent ke[KE_LENGTH]; |
155 |
struct timespec poll_time; |
156 |
PF *hdl; |
157 |
fde_t *F; |
158 |
|
159 |
/* |
160 |
* remember we are doing NANOseconds here, not micro/milli. God knows |
161 |
* why jlemon used a timespec, but hey, he wrote the interface, not I |
162 |
* -- Adrian |
163 |
*/ |
164 |
poll_time.tv_sec = 0; |
165 |
poll_time.tv_nsec = SELECT_DELAY * 1000000; |
166 |
num = kevent(kqfd.fd, kq_fdlist, kqoff, ke, KE_LENGTH, &poll_time); |
167 |
kqoff = 0; |
168 |
|
169 |
set_time(); |
170 |
|
171 |
if (num < 0) |
172 |
{ |
173 |
#ifdef HAVE_USLEEP |
174 |
usleep(50000); /* avoid 99% CPU in comm_select */ |
175 |
#endif |
176 |
return; |
177 |
} |
178 |
|
179 |
for (i = 0; i < num; i++) |
180 |
{ |
181 |
F = lookup_fd(ke[i].ident); |
182 |
if (F == NULL || !F->flags.open || (ke[i].flags & EV_ERROR)) |
183 |
continue; |
184 |
|
185 |
if (ke[i].filter == EVFILT_READ) |
186 |
{ |
187 |
if ((hdl = F->read_handler) != NULL) |
188 |
{ |
189 |
F->read_handler = NULL; |
190 |
hdl(F, F->read_data); |
191 |
if (!F->flags.open) |
192 |
continue; |
193 |
} |
194 |
} |
195 |
|
196 |
if (ke[i].filter == EVFILT_WRITE) |
197 |
{ |
198 |
if ((hdl = F->write_handler) != NULL) |
199 |
{ |
200 |
F->write_handler = NULL; |
201 |
hdl(F, F->write_data); |
202 |
if (!F->flags.open) |
203 |
continue; |
204 |
} |
205 |
} |
206 |
|
207 |
comm_setselect(F, 0, NULL, NULL, 0); |
208 |
} |
209 |
} |
210 |
#endif |